简体   繁体   English

使用C#在XML文档中检索来自不同分支的父节点的叶子

[英]Retrieving leaves from differently branched parent nodes with C# in an XML document

<Root>
  <P1 Text ="A" >
    <P2 Text = "AA">
      <P3 Text = "AAA">
        <L Text = "l_A"/>
        <L Text = "l_B"/>
        <L Text = "l_C"/>
      </P3>
      <P3 Text = "BBB">
        <L Text = "l_D"/>
        <L Text = "l_E"/>
        <L Text = "l_F"/>
      </P3>
    </P2>
    <P2 Text = "BB">
       <L Text = "l_G"/>
       <L Text = "l_H"/>
       <L Text = "l_I"/>
    </P2>
  </P1>
</Root>  

From an XML document containing thousands of variably nested nodes up to 10 levels deep, I would like to retrieve programmatically only the leaves belonging to any of the "P" parents as follows: for instance, in the example above, selecting P2 "AA" would yield l_A to l_F and P3 "BBB" would give l_D to l_F. 从包含数千个可变嵌套节点到多达10级深度的XML文档中,我想以编程方式仅检索属于任何“P”父节点的叶子,如下所示:例如,在上面的示例中,选择P2“AA”会产生l_A到l_F而P3“BBB”会产生l_D到l_F。

Something like this (returns a list of strings): 像这样的东西(返回一个字符串列表):

    XDocument doc = XDocument.Load(@"test.xml");

    string level = "P3";
    string levelAttr = "AAA";

    var list = (from d in doc.Descendants(level)
                let xAttribute = d.Attribute("Text")
                where xAttribute != null && xAttribute.Value == levelAttr
                from l in d.Descendants("L")
                let lAttribute = l.Attribute("Text")
                where lAttribute != null
                select lAttribute.Value);

You may remove the attribute null checks if the Text attribute is always there... 如果Text属性始终存在,您可以删除属性null检查...

one way is using XPath by XmlDocument (if you not using LINQ ) 一种方法是通过XmlDocument使用XPath (如果你不使用LINQ

your XPath may be like this: 你的XPath可能是这样的:

//P2[@Text='AA']//L/@Text

and your code like this: 和你的代码是这样的:

XmlDocument document; //init and load it

static List<String> GetLeavesText(int pLevel /* 2 */, string pText /* AA */)
{
    var result = new List<String>();

    //loaded document

    var nodeList = document.SelectNodes(String.Format(@"//P{0}[@Text='{1}']//L/@Text", pLevel, pText));
    if (nodeList != null)
        foreach (XmlNode xmlNode in nodeList)
        {
            result.Add(xmlNode.InnerText);
        }

    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM