从根节点读取子节点



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

我使用以下行从根节点中选择子节点,但每次读取第一个应用程序数据时。我知道我已经在下面的方法中硬编码了路径,但我不确定如何逐个读取子节点。

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>

获取联系人节点节点,然后您可以迭代每个联系人节点并获取子节点,如下所示

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

最新更新