Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML节点中的C XML元素计数_C#_Xml_Xmldocument_Xmlnode_Xmlnodelist - Fatal编程技术网

C# XML节点中的C XML元素计数

C# XML节点中的C XML元素计数,c#,xml,xmldocument,xmlnode,xmlnodelist,C#,Xml,Xmldocument,Xmlnode,Xmlnodelist,我需要得到员工标签的具体数量,以及 例如: EID - 9991515720640 with 1st EmpTypeHeader(XML Node) tag Contains 2 EmpList Tag EID - 4534545454534 with 2nd EmpTypeHeader(XML Node) tag Contains 1 EmpList Tag EID - 8998653323 with 3rd EmpTypeHeader(XML Node) tag Contains 1

我需要得到员工标签的具体数量,以及

例如:

EID - 9991515720640 with 1st EmpTypeHeader(XML Node) tag Contains 2 EmpList Tag
EID - 4534545454534 with 2nd EmpTypeHeader(XML Node) tag Contains 1 EmpList Tag
EID - 8998653323    with 3rd EmpTypeHeader(XML Node) tag Contains 1 EmpList Tag
var count = XDocument.Load("path/to/file.xml")
    .Descendants("EmpTypeHeader")
    .Where(x => (string) x.Element("EID") == "9991515720640")
    .Descendants("Position")
    .Count();
但当我选择EMPList Tag count时,它显示的总数为4

我的XML:

<Employee>
    <EmployeeHeader>
        <Date>2016-01-07</Date>
        <Time>03:45:39</Time>
    </EmployeeHeader>
    <EmpTypeHeader>
        <EID>9991515720640</EID>
        <AAA>4</AAA>
        <BBB />
        <EmpList>
            <CCC>222</CCC>
            <DDD>3333</DDD>
            <EEE>2050-09-25</EEE>
            <FFF>000</FFF>
        </EmpList>     
        <EmpList>
            <CCC>555</CCC>
            <DDD>666</DDD>
            <EEE>2050-09-25</EEE>
            <FFF>000</FFF>
        </EmpList>   
    </EmpTypeHeader>
    <EmpTypeHeader>
        <EID>4534545454534</EID>
        <AAA>66</AAA>
        <BBB />
        <EmpList>
            <CCC>999</CCC>
            <DDD>008</DDD>
            <EEE>2050-09-25</EEE>
            <FFF>000</FFF>
        </EmpList>      
    </EmpTypeHeader>
    <EmpTypeHeader>
        <EID>8998653323</EID>
        <AAA>9999</AAA>
        <BBB />
        <EmpList>
            <CCC>11333334</CCC>
            <DDD>663312</DDD>
            <EEE>2050-09-25</EEE>
            <FFF>000</FFF>
        </EmpList>      
    </EmpTypeHeader>
</Employee>

如果你能使用.NET3.5

XElement root = XElement.Load(strFileName);
int total = 0;
foreach(var eid in root.Descendants("EID").ToList())
{
   int count = eid.Parent.Elements("EmpList").Count();
   Console.WriteLine(eid.Value + " " + count.ToString());
   total += count;
}
Console.WriteLine("Total EmpList's: " + total.ToString());

我不太明白这个问题。您是要员工总数还是EID前的员工总数?我两个都做了

问题在于XPath表达式//Position将查找文档中具有该名称的所有节点,而您只对当前节点的后代感兴趣

将此位置更改为//位置-。指当前节点上下文

话虽如此,LINQ to XML是一个比XPath和旧的XmlDocument API更干净的解决方案,例如:

EID - 9991515720640 with 1st EmpTypeHeader(XML Node) tag Contains 2 EmpList Tag
EID - 4534545454534 with 2nd EmpTypeHeader(XML Node) tag Contains 1 EmpList Tag
EID - 8998653323    with 3rd EmpTypeHeader(XML Node) tag Contains 1 EmpList Tag
var count = XDocument.Load("path/to/file.xml")
    .Descendants("EmpTypeHeader")
    .Where(x => (string) x.Element("EID") == "9991515720640")
    .Descendants("Position")
    .Count();

没关系,看来我是瞎的。删除了以前的评论