【问题标题】:Retrieve list of xml tag of child nodes in linQ to xml在linQ to xml中检索子节点的xml标签列表
【发布时间】:2021-11-20 07:14:37
【问题描述】:

我正在尝试将加载的 XML 的不同子节点(不是从根开始)的所有列表获取到字符串列表中,我已经使用 System.Xml 库完成了,但我想编写相同的代码LINQ to XML 也是。 我找到了一个对我帮助很大的代码,但它是从 Root 开始的,代码如下:

List<string> nodesNames = new List<string>();
XDocument xdoc1 = XDocument.Load(destinationPath);
XElement root = xdoc1.Document.Root;

foreach (var name in root.DescendantNodes().OfType<XElement>()
        .Select(x => x.Name).Distinct())
{
        if (!nodesNames.Contains(name.ToString()))
        nodesNames.Add(name.ToString());
}

有了这个,我也得到了所有子节点 + 父节点的列表,我不想使用它。FirstChild 或从列表中手动删除,因为我想要一个完全动态的代码并且我输入了用户传递的父节点。

为了更好地理解,这是对我有用但使用 System.Xml 的代码:

List<string> nodesNames = new List<string>();
XmlDocument doc = new XmlDocument();
doc.Load(destinationPath);
XmlNodeList elemList = doc.GetElementsByTagName(inputParentNode);

for (int i = 0; i < elemList.Count; i++)
{
        XmlNodeList cnList = (elemList[i].ChildNodes);
        for (int j = 0; j < cnList.Count; j++)
        {
                string name = cnList[j].Name;
                if (!nodesNames.Contains(name))
                        nodesNames.Add(name);
        }
}

这是一个简单的 XML 示例:

<?xml version='1.0' encoding='UTF-8'?>
<parentlist>
        <parent>
                <firstchild>someValue</firstchild>
                <secondchild>someValue</secondchild>
        </parent>
        <parent>
                <firstchild>someValue</firstchild>
                <secondchild>someValue</secondchild>
                <thirdchild>someValue</thirdchild>
        </parent>
</parentlist>

继续:

  • 在第一种情况下,我获得了 nodesNames = ["parent", "firstchild", "secondchild", "thirdchild"]
  • 在第二种情况下,我获得了 nodesNames = ["firstchild", "secondchild", "thirdchild"]

我只想修复第一个以获得与第二个相同的结果。

【问题讨论】:

    标签: c# xml linq-to-xml xelement descendant


    【解决方案1】:

    尝试以下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
               XDocument doc = XDocument.Load(FILENAME);
               XElement parentlist= doc.Root;
               List<string> descendents = parentlist.Descendants().Where(x => x.HasElements).Select(x => string.Join(",", x.Name.LocalName, string.Join(",", x.Elements().Select(y => y.Name.LocalName)))).ToList();
    
    
            }
        }
    }
    

    【讨论】:

    • 谢谢你的回答,但我这里也有同样的问题!我刚刚测试过,它返回第一个父级 ["parent"、"firstchild"、"secondchild"、"thirdchild"] 内所有节点的完整列表,我需要使用 inputNode 来探索这个 xml 的第二级用户在这种方法中,只获取自己内部的节点:[“firstchild”,“secondchild”,“thirdchild”] 就像我在上面发布的第二个代码一样,看着我的XML,就像我必须从父级开始而不是来自父母名单,但我不知道如何使它成为可能!
    • 您确定要从其他节点获取子节点吗?该代码只是解析父元素。
    • 试试:parentlist.Elements().Descendants()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多