获取 XML 节点下 xml 元素的数量 [英] Getting the count of xml elements under an XML Node

查看:39
本文介绍了获取 XML 节点下 xml 元素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望获取 XML 文件中特定节点下的元素计数.

I am looking to get the count of elements under a particular node in an XML file.

该文件将类似于以下内容

The file will look something like the following

<Return>
  <ReturnHeader>   
  </ReturnHeader>
  <ReturnData documentCnt="8">
    <file1></file1>   
    <file2></file2>   
    <file3></file3>   
    <file4></file4>   
    <file5></file5>   
    <file6></file6>   
    <file7></file7>   
    <file8></file8>
  </ReturnData>
<ParentReturn>
  <ReturnHeader> 
  </ReturnHeader>
  <ReturnData documentCnt="6">
    <file1></file1>   
    <file2></file2>   
    <file3></file3>   
    <file4></file4>   
    <file5></file5>   
    <file6></file6>     
  </ReturnData>
</ParentReturn>
<SubsidiaryReturn>
  <ReturnHeader>
  </ReturnHeader>
  <ReturnData documentCnt="3">
    <file1></file1>   
    <file2></file2>   
    <file3></file3>     
  </ReturnData>
</SubsidiaryReturn>
</Return>

我需要为 ReturnData 节点(如您所见位于文件中的多个位置)解析此 xml 文件,并获取其下元素的计数.

I need to parse this xml file for the ReturnData node (which is located in multiple locations in the file as you can see) and get the count of the elements underneath it.

例如- 在 Return\ReturnData 下,计数必须为 8- 在 Return\ParentReturn\ReturnData 下,计数必须为 6- 在 Return\SubsidiaryReturn\ReturnData 下,计数必须为 3

For example - Under Return\ReturnData the count has to be 8 - Under Return\ParentReturn\ReturnData the count has to be 6 - Under Return\SubsidiaryReturn\ReturnData the count has to be 3

属性 documentCnt 实际上应该给我正确的计数,但是创建的 xml 文档会有差异,因此我需要解析这个 xml 文件并检查 documentCnt 属性中的值是否与 ReturnData 下的元素数量匹配节点.

The attribute documentCnt should actually give me the right count but the xml document that gets created will have discrepancies and hence I will need to parse this xml file and check if the value in the documentCnt attribute matches the number of elements under the ReturnData node.

推荐答案

使用你给出的问题描述:

Using the problem description you gave of:

属性 documentCnt 实际上应该给我正确的计数但是创建的 xml 文档会有差异,因此我将需要解析此 xml 文件并检查documentCnt 属性匹配下的元素数量返回数据节点.

The attribute documentCnt should actually give me the right count but the xml document that gets created will have discrepancies and hence I will need to parse this xml file and check if the value in the documentCnt attribute matches the number of elements under the ReturnData node.

如果您要在ReturnData"元素上使用简单的选择语句,这可以在一个步骤中解决,如下所示:

This could be solved in a single step if you were to use a simple select statement on the "ReturnData" element, as in:

public static void Main(params string[] args)
{
    // test.xml contains OPs example xml.
    var xDoc = XDocument.Load(@"c:\temp\test.xml");

    // this will return an anonymous object for each "ReturnData" node.
    var counts = xDoc.Descendants("ReturnData").Select((e, ndx) => new
    {
        // although xml does not have specified order this will generally
        // work when tracing back to the source.
        Index = ndx,

        // the expected number of child nodes.
        ExpectedCount = e.Attribute("documentCnt") != null ? int.Parse(e.Attribute("documentCnt").Value) : 0,

        // the actual child nodes.
        ActualCount = e.DescendantNodes().Count()
    });

    // now we can select the mismatches
    var mismatches = counts.Where(c => c.ExpectedCount != c.ActualCount).ToList();

    // and the others must therefore be the matches.
    var matches = counts.Except(mismatches).ToList();

    // we expect 3 matches and 0 mismatches for the sample xml.
    Console.WriteLine("{0} matches, {1} mismatches", matches.Count, mismatches.Count);
    Console.ReadLine();
}

这篇关于获取 XML 节点下 xml 元素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆