2011-04-26 9 views
1

最後に、よく言われていない質問を削除し、最も簡単な形にしました。私はルートノードを選択しようとしていますが、それはnullとして戻ってきます。XmlElementからルートノードを選択できません

は、ここに私のコードですここ

XmlDocument input = new XmlDocument(); 
XmlDocument output = new XmlDocument(); 

input.Load(@"simple.xml"); 

XmlNode outputNode = output.CreateNode(XmlNodeType.Element, input.ChildNodes[1].Name, null); 

Console.WriteLine(outputNode.SelectSingleNode("root") == null ? "null" : "node found"); 
Console.WriteLine(outputNode.SelectSingleNode("/root") == null ? "null" : "node found"); 
Console.WriteLine(outputNode.SelectSingleNode("//root") == null ? "null" : "node found"); 

//After doing this, /root and //root return the root node 
output.AppendChild(node); 

Console.WriteLine(outputNode.SelectSingleNode("root") == null ? "null" : "node found"); 
Console.WriteLine(outputNode.SelectSingleNode("/root") == null ? "null" : "node found"); 
Console.WriteLine(outputNode.SelectSingleNode("//root") == null ? "null" : "node found"); 

EDIT私のXML

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <children> 
    <child>Clark</child> 
    <child>Bruce</child> 
    <child>Peter</child> 
    </children> 
</root> 

だと:@Marcは、右のトラックに私を置きます。実際にノードをXmlDocumentに追加すると、そのノードは正常に動作しました。

+1

する

は、実際には理由.ChildNodesのあなたの子供のノードであるかもしれない[1] ...多分一見の価値。 – Craig

答えて

1

子孫のない新しい孤児ノード(つまり、まだツリーにはありません)が作成されました。その孤児のに関連するクエリは何も見つかりません。それは、出力ノードのように私には見えます、既存のノードを見つける.DocumentElementを見て、.SelectSingleNode(...)SelectNodes(...)

関連する問題