Search code examples
c#xmlxmldocumentxmlnodexmlnodelist

C# XML Element Count in XML Node


I need to get the count of EmpList Tag in Particular <EmpTypeHeader> and <EID>.

For example:

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

But when I select EMPList Tag count it shows a total Count of 4.

My 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>

And my code:

private void ReadXMLEmp()
{
    string eid = "9991515720640";
    string strFileName = @"D:\Raseeth\Test1.xml";
    xmlDocument = new XmlDocument();

    xmlDocument.Load(strFileName);
    xmlNodeListEmpTypeHeader = xmlDocument.SelectNodes("//EmpTypeHeader");                    
    if (xmlNodeListEmpTypeHeader != null)
    {
        int empListCount = 0;

        foreach (XmlNode xmlNodeEmpTypeHeader in xmlNodeListEmpTypeHeader)
        {
            if (xmlNodeEmpTypeHeader["EID"] != null && xmlNodeEmpTypeHeader["EID"].InnerText.Trim() == eid)
            {
                empListCount = xmlNodeEmpTypeHeader.SelectNodes("//Position").Count; 
                Console.WriteLine("EmpList Count : " + empListCount);
                bFlag = true;
                break;
            }
        }
    }
}

Solution

  • The issue is your XPath expression. //Position will find all nodes in the document with that name, whereas you are only interested in descendants from the current node.

    Change this to .//Position - the . refers to the current node context.

    Having said this, LINQ to XML is a far cleaner solution that XPath and the old XmlDocument API, for example:

    var count = XDocument.Load("path/to/file.xml")
        .Descendants("EmpTypeHeader")
        .Where(x => (string) x.Element("EID") == "9991515720640")
        .Descendants("Position")
        .Count();