2017-03-22 243 views
0

我需要从下面的xml读取子节点,并需要将数据插入到数据库中。从根节点读取子节点

我正在使用下面一行从根节点中选择子节点,但每次读取第一个应用程序data.I知道我硬编码在下面的方法中的路径,但我不知道如何读取子节点逐个。

XmlNodeList boxNodeList = document.SelectSingleNode("applications/application/contacts").ChildNodes; 

示例XML

<applications> 
    <application > 
     <contacts> 
      <business-owner> 
       <a></a> 
       <b></b> 
       <c> </c> 
      </business-owner> 
      <it-owner> 
       <a></a> 
       <b></b> 
       <c></c> 
      </it-owner> 
      <architect> 
       <a></a> 
       <b></b> 
       <c></c> 
      </architect> 
      <dataContact> 
       <a></a> 
       <b></b> 
       <c></c> 
      </dataContact> 
      <technical> 
       <a></a> 
       <b></b> 
       <c> </c> 
      </technical> 
      <technical> 
       <a></a> 
       <b></b> 
       <c> </c> 
      </technical> 
      <other> </other> 
     </contacts> 
      </application> 
    <application > 
     <contacts> 
      <business-owner> 
       <a></a> 
       <b></b> 
       <c> </c> 
      </business-owner> 
      <it-owner> 
       <a></a> 
       <b></b> 
       <c> </c> 
      </it-owner> 
      <technical> 
       <a></a> 
       <b></b> 
       <c> </c> 
      </technical> 
      <other/> 
     </contacts> 

    </application> 
    <application > 
     <contacts> 
      <business-owner> 
       <a></a> 
       <b></b> 
       <c> </c> 
      </business-owner> 
      <it-owner> 
       <a></a> 
       <b></b> 
       <c> </c> 
      </it-owner> 
      <other/> 
     </contacts> 

    </application> 
    </applications> 

回答

0

GET接触节点的节点,然后你可以遍历每个接触点,并获得子节点如下

XmlNodeList nodes= document.SelectNodes("applications/application/contacts"); 
foreach(XmlNode node in nodes) 
{ 
    XmlNodeList boxNodeList = node.ChildNodes; 
    // do something with boxNodeList 
}