2012-08-01 3 views
14

私はXMLファイルを持っており、各子ノードは情報を収集していますが、繰り返したいと思います。XMLファイル内の各子ノードはどのように反復処理できますか?

ここではC#コードは1つのノードだけをピックアップし、FieldDataはその子ノードでforeachを使用したいと考えています。

<?xml version="1.0"?> 
<FieldData> 
    <property_details_branch IncludeInPDFExport="Yes" Mod="20010101010101"/> 
    <property_details_inspection_date IncludeInPDFExport="Yes" Mod="20120726200230">20120727220230+0200</property_details_inspection_date> 
    <property_details_type_of_ownership IncludeInPDFExport="Yes" Mod="20120726134107">Freehold</property_details_type_of_ownership> 
</FieldData> 
+0

私のXMLで子ノードに、さらにプッシュするために以下のコードを使用しましたそれを0に戻します。 –

答えて

18

あなたはFieldDataノードを反復している、あなたが1つしかない:

public void LoadXML() { 
    if (File.Exists("Data.xml")) { 
     //Reading XML 
     XmlDocument xmlDoc = new XmlDocument(); 
     xmlDoc.Load("Data.xml"); 

     //Think something needs to reference Child nodes, so i may Foreach though them 

     XmlNodeList dataNodes = xmlDoc.SelectNodes("//FieldData"); 
     TagContents[] ArrayNode; 

     foreach(XmlNode node in dataNodes) { 
      int Count = 0; 
      //int Max = node.ChildNodes.Count; 
      ArrayNode = new TagContents[Max]; 

      ArrayNode[Count].TagName = node.Name; 
      ArrayNode[Count].TagValue = node.SelectSingleNode(ArrayNode[Count].TagName).InnerText; 
      Count = Count + 1;   
     } 
    } else { 
     MessageBox.Show("Could not find file Data.xml"); 
    } 
} 

私のXMLは次のようになります。反復処理するには、その子ノードは書き込み:

var doc = XDocument.Load("XMLFile1.xml"); 
    foreach (var child in doc.Element("FieldData").Elements()) 
    { 
    Console.WriteLine(child.Name); 
    } 
+1

これはforeach(node.ChildNodesのXmlNode childNode)であるべきではありませんか? – Rox

+0

あなたはそうです。私はXDocumentに慣れています –

+0

ありがとうございます:私はずっと苦労しました。 – Pomster

5

が、私は一般的にこの種のもののためにLinq-To-Xmlを好む

XDocument doc = XDocument.Load(@"Data.xml"); 
    TagContents[] ArrayNode = doc.Root 
           .Elements() 
           .Select(el => 
            new TagContents() 
            { 
             TagName = el.Name.ToString(), 
             TagValue = el.Value 
            }) 
           .ToArray(); 
2

あなたはこのようにそれを行うことができます:

foreach (XmlNode node in dataNodes) 
{ 
    foreach (XmlNode childNode in node.ChildNodes) 
    { 
4

それとも、再帰を使用します。

public void findAllNodes(XmlNode node) 
    { 
     Console.WriteLine(node.Name); 
     foreach (XmlNode n in node.ChildNodes) 
      findAllNodes(n); 
    } 

ペイロードの配置場所は、使用する検索の種類(例:幅優先検索、深さ優先検索などが含まれる。 http://en.wikipedia.org/wiki/Euler_tour_techniqueを参照してください)

0

@Waynesの回答に触れて、うまくいきました。私はインクリメント、私はちょうどあなたがゼロに `Count`を設定し、あなたの` foreach`ループの中で、ものを行うことに注意したい

foreach (var child in doc.Element("rootnodename").Element("nextchildnode").Elements()) 

{ 

//do work here, probs async download xml content to file on local disc 

} 
0
public void ValidateXml(string[] Arrays) 
    {           
     foreach (var item in Arrays) 
     { 
      Xdoc.Load(item);        
      XmlNodeList xnList = Xdoc.SelectNodes("FirstParentNode"); 
      if (xnList.Count > 0) 
      { 
       foreach (XmlNode xn in xnList) 
       { 
        XmlNodeList anode = xn.SelectNodes("SecondParentNode"); 
        if (anode.Count > 0) 
        { 
         foreach (XmlNode bnode in anode) 
         {        
          string InnerNodeOne = bnode["InnerNode1"].InnerText; 
          string InnerNodeTwo = bnode["InnerNode1"].InnerText; 

         }       
        } 
        else 
        { 
         ErrorLog("Parent Node DoesNot Exists");             
        } 
       }     
      } 
      else 
      { 
       ErrorLog("Parent Node DoesNot Exists"); 
      } 

     } 
     //then insert or update these values in database here 
    } 
関連する問題