C#如何查找XML文档子记录计数。

huangapple go评论61阅读模式
英文:

C# how to find xml doc child record count

问题

以下是翻译好的部分:

格式1

<a>
  <b/>
</a>

格式2

<a>
  <b>
    <c>
      <c1>数据</c1>
      <c2>数据1</c2>
    </c>
  </b>
</a>

在C#中,您可以使用XmlDocument类来解析XML并检查C记录是否存在以及获取C记录的计数。以下是一个示例代码片段:

using System;
using System.Xml;

class Program
{
    static void Main()
    {
        string xml = @"
            <a>
              <b>
                <c>
                  <c1>Data</c1>
                  <c2>Data1</c2>
                </c>
              </b>
            </a>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);

        XmlNodeList cNodes = doc.SelectNodes("//c");
        
        if (cNodes != null && cNodes.Count > 0)
        {
            Console.WriteLine("C记录存在,数量为: " + cNodes.Count);
        }
        else
        {
            Console.WriteLine("C记录不存在。");
        }
    }
}

请注意,此示例假定您已经将XML数据加载到名为"xml"的字符串变量中。您可以根据您的实际需求更改XML数据和检查逻辑。

英文:

I have a xml in below both formats

format 1

&lt;a&gt;
  &lt;b/&gt;
&lt;/a&gt;

format 2

&lt;a&gt;
  &lt;b&gt;
    &lt;c&gt;
      &lt;c1&gt;Data&lt;/c1&gt;
      &lt;c2&gt;Data1&lt;/c2&gt;
    &lt;/c&gt;
  &lt;/b&gt;
&lt;/a&gt; 

How can I write c# code to see is c record exist or not and if exist how can take a count of c record?

答案1

得分: 2

使用 XDocument,您可以像这样操作。如果未找到任何元素,Count 将返回子元素的数量,或者返回 0。

using System;
using System.Linq;
using System.Xml.Linq;

var format1 = &quot;&quot;&quot;
    &lt;a&gt;
      &lt;b/&gt;
    &lt;/a&gt;
    &quot;&quot;&quot;

var format2 = &quot;&quot;&quot;
    &lt;a&gt;
      &lt;b&gt;
        &lt;c&gt;
          &lt;c1&gt;数据&lt;/c1&gt;
          &lt;c2&gt;数据1&lt;/c2&gt;
        &lt;/c&gt;
      &lt;/b&gt;
    &lt;/a&gt;
    &quot;&quot;&quot;

Console.WriteLine(Count(format1, &quot;c&quot;)); // 打印 0
Console.WriteLine(Count(format2, &quot;c&quot;)); // 打印 2

int Count(string xml, string elementName)
{
    var document = XDocument.Parse(xml);
    var count = document.Descendants(elementName).Elements().Count();
    return count;
}
英文:

Using XDocument you can do something like this. Count will return the number of child elements or 0 if no elements was found.

using System;
using System.Linq;
using System.Xml.Linq;

var format1 = &quot;&quot;&quot;
    &lt;a&gt;
      &lt;b/&gt;
    &lt;/a&gt;
    &quot;&quot;&quot;;

var format2 = &quot;&quot;&quot;
    &lt;a&gt;
      &lt;b&gt;
        &lt;c&gt;
          &lt;c1&gt;Data&lt;/c1&gt;
          &lt;c2&gt;Data1&lt;/c2&gt;
        &lt;/c&gt;
      &lt;/b&gt;
    &lt;/a&gt;
    &quot;&quot;&quot;;

Console.WriteLine(Count(format1, &quot;c&quot;)); // Prints 0
Console.WriteLine(Count(format2, &quot;c&quot;)); // Prints 2

int Count(string xml, string elementName)
{
    var document = XDocument.Parse(xml);
    var count = document.Descendants(elementName).Elements().Count();
    return count;
}

答案2

得分: 0

以下函数将从XElement中找到您想要的内容:

static int FindC(XElement root)
{
    return root
        .Elements("b")
        .SelectMany(n => n.Elements("c"))
        .SelectMany(n => n.Elements())
        .Count();
}

使用方法是首先将XML解析为XDocument,然后传递document.Root

var doc = XDocument.Parse(yourXml);
var count = FindC(doc.Root);

您也可以使用XPath。例如:

var countOfAnyC = xdoc.Root.XPathSelectElements("//c").Count();
var countOfABC = xdoc.Root.XPathSelectElements("/a/b/c").Count();

dotnetfiddle

英文:

The following function will find what you want from a XElement

static int FindC(XElement root)
{
	return root
		.Elements(&quot;b&quot;)
		.SelectMany(n =&gt; n.Elements(&quot;c&quot;))
		.SelectMany(n =&gt; n.Elements())
		.Count();
}

Use it by first parsing the XML into XDocument, then passing document.Root

var doc = XDocument.Parse(yourXml);
var count = FindC(doc.Root);

dotnetfiddle

You can also use XPath. For example:

var countOfAnyC = xdoc.Root.XPathSelectElements(&quot;//c&quot;).Count();
var countOfABC = xdoc.Root.XPathSelectElements(&quot;/a/b/c&quot;).Count();

huangapple
  • 本文由 发表于 2023年7月14日 05:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定