2009-06-14 17 views
0

ようで、私はXMLドキュメントC#SelectSingleNode - 再帰的に使用できますか?

<root a="value"> 
    <item name="first"> 
     x 
     <foo name = "firstgrandchild">There is nothing here</foo> 
     y 
     <foo name = "secondgrandchild">There is something here</foo> 
    </item> 
    <item name="second"> 
     xy 
     <foo/> 
     ab 
    </item> 
</root> 

を持っている場合、私は最初に「foo「をアイテム」ノードの最初の出現を検索し、属性を更新した後、私はノードの最初の出現を更新したいしたいです「その後、

マイコードは

myDoc.Load("Items2.xml"); 
myNode = myDoc.DocumentElement; 
mySearchNode = myNode.SelectSingleNode("/root/item"); 
mySearchNode.Attributes["name"].Value = "Joel"; 
Console.WriteLine(mySearchNode.OuterXml); 
mySearchChildNode = mySearchNode.SelectSingleNode("/item/foo"); 
Console.WriteLine(mySearchChildNode.OuterXml); 

しながら、以下の通りである、などの属性を更新し、属性の最初の検索や更新が正常に動作し、もう一つはmySearchNode.SelectSingleNodeがnullを返すよう失敗します。

質問 - 基本的にこのコードに間違いがありますか? 2番目のインスタンスでSelectSingleNodeが期待通りに機能しないのはなぜですか、それは要素のXmlNodeで実行しています。

親切に助けてください。

多くのおかげで、

答えて

5

あなたの第二のXPathクエリは先頭のスラッシュなしであるべきである必要があります。/"ドキュメントのルート"を意味します。スラッシュを省略すると、クエリはmySearchNode変数に相対的になります。また、 "item"を再度含めるべきではありません。クエリは、選択した "item"ノードに関連しています。コード内:

myDoc.Load("Items2.xml"); 
myNode = myDoc.DocumentElement; 
mySearchNode = myNode.SelectSingleNode("/root/item"); 
mySearchNode.Attributes["name"].Value = "Joel"; 
Console.WriteLine(mySearchNode.OuterXml); 
mySearchChildNode = mySearchNode.SelectSingleNode("foo"); 
Console.WriteLine(mySearchChildNode.OuterXml); 
2

mySearchNodeはitemノードなので、fooitemの子である場合、2番目のXPathは単に"foo"

0

すべてのアイテムノードをループすることができます。各項目について、さらにfooノードでSelectNodesを実行する必要があります。アイテム数とfooノードの両方について、ノード数と属性名が存在するかどうかをチェックする必要があります。 このコードを使用できます:

/// <summary> 
/// Changes the xml in sourceFileName and writes the changed xml to destFileName 
/// </summary> 
public static void ProcessNames(string sourceFileName, string destFileName) 
{ 
    XmlDocument xmlDoc = new XmlDocument(); 
    XmlTextWriter xtw = null; 
    xmlDoc.Load(sourceFileName); 

    try 
    { 
     // Parse through all the item nodes and process them 
     XmlNodeList itemList = xmlDoc.SelectNodes("//root/item"); 

     if (itemList.Count > 0) 
     { 
      foreach (XmlNode item in itemList) 
      { 
       // Change the name attribute, if it exists 
       if (item.Attributes["name"] != null) 
       { 
        string newItemName = item.Attributes["name"].Value + "Joel"; 
        item.Attributes["name"].Value = newItemName; 
       } 

       // Parse through all the foo nodes and process them 
       XmlNodeList fooList = item.SelectNodes("foo"); 

       if (fooList.Count > 0) 
       { 
        foreach (XmlNode foo in fooList) 
        { 
         // Change the name attribute, if it exists 
         if (foo.Attributes["name"] != null) 
         { 
          string newFooName = foo.Attributes["name"].Value + "Joel"; 
          foo.Attributes["name"].Value = newFooName; 
         } 
        } 
       } 

      } 

      xtw = new XmlTextWriter(destFileName, Encoding.UTF8); 
      xmlDoc.WriteContentTo(xtw); 
     } 

    } 
    finally 
    { 
     xtw.Close(); 
    } 
} 
関連する問題