如何在XML及其所有子节点中查找特定节点?

时间:2013-03-31 06:18:19

标签: c# xml

这是我的XML:

<?xml version="1.0"?>
<formatlist>
<format>
    <formatName>WHC format</formatName>
    <delCol>ID</delCol>
    <delCol>CDRID</delCol>
    <delCol>TGIN</delCol>
    <delCol>IPIn</delCol>
    <delCol>TGOUT</delCol>
    <delCol>IPOut</delCol>
    <srcNum>SRCNum</srcNum>
    <distNum>DSTNum</distNum>
    <connectTime>ConnectTime</connectTime>
    <duration>Duration</duration>
</format>
<format>
    <formatName existCombineCol="1">Umobile format</formatName>   //this format
    <delCol>billing_operator</delCol>
    <hideCol>event_start_date</hideCol>
    <hideCol>event_start_time</hideCol>
    <afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss">  //node i want
        <name>ConnectdateTimeAFcombine</name>
        <combineDate>event_start_date</combineDate>
        <combineTime>event_start_time</combineTime>
    </afCombineName>
    <afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss">  //node i want
        <name>aaa</name>
        <combineDate>bbb</combineDate>
        <combineTime>ccc</combineTime>
    </afCombineName>
    <modifyPerfixCol action="add" perfix="60">bnum</modifyPerfixCol>
    <srcNum>anum</srcNum>
    <distNum>bnum</distNum>
    <connectTime>ConnectdateTimeAFcombine</connectTime>
    <duration>event_duration</duration>
</format>
</formatlist>

我想找到Umobile格式的格式,然后迭代这两个节点。

<afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss">  //node i want
    <name>ConnectdateTimeAFcombine</name>
    <combineDate>event_start_date</combineDate>
    <combineTime>event_start_time</combineTime>
</afCombineName>
<afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss">  //node i want
    <name>aaa</name>
    <combineDate>bbb</combineDate>
    <combineTime>ccc</combineTime>
</afCombineName>

并列出所有两个节点的子节点。结果应该是这样的:

ConnectdateTimeAFcombine,event_start_date,event_start_time.
aaa,bbb,ccc

我该怎么做?

4 个答案:

答案 0 :(得分:0)

foreach(var children in format.Descendants())

  {

     //Do something with the child nodes of format.
  }

答案 1 :(得分:0)

对于所有与XML相关的遍历,您应该习惯使用XPath表达式。这非常有用。即使您可以在特定情况下更轻松地执行某些操作,但最好还是使用XPath。这样,如果您的方案在某些时候发生了变化,您只需更新XPath表达式,您的代码就会启动并运行。

有关完整示例,您可以查看this article

答案 2 :(得分:0)

您可以将System.Xml命名空间API与System.Xml.XPath命名空间API一起使用。这是一个快速算法,可以帮助您完成任务:

  1. 使用以下XPATH获取包含字符串 Umobile格式的文本节点:

    XmlNode umobileFormatNameNode = document.SelectSingleNode(“// formatName [text()='Umobile format']”);

  2. 现在umobileFormatNameNode的父节点将是您感兴趣的节点:

    XmlNode formatNode = umobileFormatNameNode.ParentNode;

  3. 现在获取此节点的子节点:

    XmlNodeList afCombineFormatNodes = formatNode.SelectNodes(“afCombineName”);

  4. 您现在可以处理afCombineFormatNodes列表

    for(afCombineNameFormtNodes中的XmlNode xmlNode) {      //处理节点 }

答案 3 :(得分:0)

通过这种方式,您可以访问这些元素:

var doc = System.Xml.Linq.XDocument.Load("PATH TO YOUR XML FILE");
var result = doc.Descendants("format")
            .Where(x => (string)x.Element("formatName") == "Umobile format")
            .Select(x => x.Element("afCombineName"));

然后你可以用这种方式迭代result

foreach (var item in result)
{
    string format = item.Attribute("format").Value.ToString();
    string name = item.Element("name").Value.ToString();
    string combineDate = item.Element("combineDate").Value.ToString();
    string combineTime = item.Element("combineTime").Value.ToString();
}