if you know the childnodes ,try
//Document/node[subnode1]//Document/node[subnode2]//Document/node[subnode3 and not(subnode2)]for example:using System;
using System.Xml;class TesNode
{
static void Main()
{string s = @"<Document>
<node>
<subnode1>1</subnode1>
</node>
<node>
<subnode2>2</subnode2>
<subnode3>3</subnode3>
</node>
<node>
<subnode3>4</subnode3>
</node>
</Document>"; XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(s); XmlNode node = xmldoc.SelectSingleNode("//Document/node[subnode1]");
Console.WriteLine(node.InnerXml); node = xmldoc.SelectSingleNode("//Document/node[subnode2]");
Console.WriteLine(node.InnerXml); node = xmldoc.SelectSingleNode("//Document/node[subnode3 and not(subnode2)]");
Console.WriteLine(node.InnerXml); }
}
//Document/node[subnode1]//Document/node[subnode2]//Document/node[subnode3 and not(subnode2)]for example:using System;
using System.Xml;class TesNode
{
static void Main()
{string s = @"<Document>
<node>
<subnode1>1</subnode1>
</node>
<node>
<subnode2>2</subnode2>
<subnode3>3</subnode3>
</node>
<node>
<subnode3>4</subnode3>
</node>
</Document>"; XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(s); XmlNode node = xmldoc.SelectSingleNode("//Document/node[subnode1]");
Console.WriteLine(node.InnerXml); node = xmldoc.SelectSingleNode("//Document/node[subnode2]");
Console.WriteLine(node.InnerXml); node = xmldoc.SelectSingleNode("//Document/node[subnode3 and not(subnode2)]");
Console.WriteLine(node.InnerXml); }
}
.....node (xpath=//Document/node)
........subnode1 (xpath=//Document/node/subnode1)
.....node (xpath=//Document/node)
........subnode2 (xpath=//Document/node/subnode2)
........subnode3 (xpath=//Document/node/subnode3)
.....node (xpath=//Document/node)
........subnode3 (xpath=//Document/node/subnode3)node都一样当然不知道你选的哪一个了,不知道可不可以保存成xpath=//document/nodes[0]之类的
..Document (xpath=//Document)
.....node name=par(xpath=//Document/node[@name='par'])
.....node (xpath=//Document/node)
.....node (xpath=//Document/node)
........subnode name=response (xpath=//Document/node/subnode[@name='response'])
..Document (xpath=//Document)
.....node name=cust(xpath=//Document/node[@name='cust'])
.....node name=main(xpath=//Document/node[@name='main'])先选定了要复制的是第1个Document下面的那个没有属性和子节点的node,然后插入到第2个Document下面的node name=main的前面。我先得到要复制的xpath=//Document/node,得到xmlnode = XmlDoc.SelectSingleNode(xpath);
接着知道要插入的node的refxpath=//Document/node[@name='main'],得到refnode = XmlDoc.SelectSingleNode(refxpath);
然后取得要插入node的parentnode,parxpath=//Document,得到parnode = XmlDoc.SelectSingleNode(parxpath);
最后调用parnode.InsertBefore(xmlnode , refnode);第一个问题是在xmlnode = XmlDoc.SelectSingleNode(xpath)的时候,我得到的不是我要的那个node,而是他的前一个node。
第二个问题是在parnode = XmlDoc.SelectSingleNode(parxpath)的时候,我得到的也不是我要的parnode,而也是之前的一个parnode。该怎么改写呢?
是否可以写成
xpath=//Document/node[attribute is null],xpath=//Document/node[subnode is null],xpath=//Document/node[attribute and subnode are all null],
或者xpath=//Document/node[attribute is null][subnode[@name='response']]可以在xpath里面正确的表达出没有属性或者没有子节点吗?
--------------------------------------------------------->
直接表示出来好像不行。
应该要判断一下的:
if(currentNode.HasChildNodes)
{
}else{
}
不过就算都可以表示了,还发现一个问题,就是如果2个node一模一样,就是他的属性和子节点一样,父节点也是一样的,那么这样还是不能判断出来。就是说我copy了一个别的节点,然后要粘贴在2个一样节点的第2个下面,很可能就贴到第1个下面去了。