简体   繁体   English

计算 XML 文档中的父节点

[英]Count parent nodes in XML Document

I am trying to count all the parent nodes in a XML Document.我正在尝试计算 XML 文档中的所有父节点。 I have seen solutions like .SelectNodes("general/parent").Count but that is a fixed solution.我见过像.SelectNodes("general/parent").Count这样的解决方案,但这是一个固定的解决方案。 The problem is that I must do it with a generated XML document.问题是我必须使用生成的 XML 文档来完成。 So I don't know the XML structure.所以我不知道 XML 结构。

I created an example.我创建了一个例子。 Imagine that the following document is generated, without knowing any tag names or information at all:想象一下,生成了以下文档,根本不知道任何标签名称或信息:

<?xml version="1.0" encoding="UTF-8"?>
<general>
    <settings>
        <resolution>1920x1080</resolution>
        <version>1.0</version>
    </settings>
    <data>
        <persons>
            <person>
                <name>Bob</name>
                <age>41</age>
            </person>
            <person>
                <name>Alex</name>
                <age>25</age>
            </person>
        </persons>
    </data>
</general> 

I want to go through this document and have a result of: 5. As the document has 5 "parents" (general, settings, data, persons, and person).我想通过这个文件并得到以下结果: 5. 由于该文件有 5 个“父项”(常规、设置、数据、人员和人员)。 But it doesn't count "resolution", "version", "name" and "age" because they have no childs (are no parents).但这不包括“分辨率”、“版本”、“姓名”和“年龄”,因为他们没有孩子(没有父母)。 But once again, keep in mind that the document is generated!但再次提醒,文档是生成的!

I hope this question is clear enough.我希望这个问题足够清楚。 Is there a way of achieving this?有没有办法实现这一目标?

使用 LinqToXml,您可以执行以下操作:

XDocument.Parse(@"...").Descendants().Where(n => n.Elements().Any()).Select(n => n.Name).Distinct().Count();

You need a recursive algorithm, like this one;你需要一个递归算法,就像这个;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace GetMaxXMLDepth
{
    class Program
    {
        static void Main(string[] args)
        {
            string doc = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<general>
    <settings>
        <resolution>1920x1080</resolution>
        <version>1.0</version>
    </settings>
    <data>
        <persons>
            <person>
                <name>Bob</name>
                <age>41</age>
            </person>
            <person>
                <name>Alex</name>
                <age>25</age>
            </person>
        </persons>
    </data>
</general>";
            var xd = XDocument.Parse(doc);

            int maxDepth = GetMaxChildDepth(xd.Root);
        }

        static int GetMaxChildDepth(XElement element)
        {
            int result = 1;

            //always return 1 as the root node has depth of 1.
            //in addition, return the maximum depth returned by this method called on all the children.
            if (element.HasElements)
                result += element.Elements().Max(p => GetMaxChildDepth(p));

            return result;
        }
    }
}
int count = ParentNode.SelectNodes(ChildNodesXPath).Count;                    

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

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