2013-03-31 56 views
0

这是我的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 

我该怎么做?

回答

0
foreach(var children in format.Descendants()) 

    { 

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

对于所有与XML相关的遍历,您应该习惯使用XPath表达式。这非常有用。即使你可以在你的特定情况下做更简单的事情,使用XPath也是很好的做法。这样,如果您的方案在某些时候发生了变化,您只需更新XPath表达式,并且代码将启动并运行。

举个完整的例子,你可以看看this article

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. 现在,您可以处理(在afCombineNameFormtNodes的XmlNode的xmlNode) {// 工艺节点 }

+0

谢谢你,你是对的。我可以像这样使用Xpath:XmlNodeList umobileFormatNameNode = document.SelectNodes(“// formatName [text()='Umobile format']/afCombineName”); – user2147152

+0

抱歉,延迟响应...是的,你可以直接以你提出的方式获取节点列表。 –

0

这样你就可以访问这些元素afCombineFormatNodes

名单:

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 th是方式:

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(); 
} 
相关问题