-1

I want to iterate through this XML-file (XmlDocument) and count the number of subnodes each Employee Element have. I want to Iterate through Employee with ID 1, count the number of sub nodes and then save it to a HashSet with the Employee ID as key, and the amount of sub nodes as value. My problem is, how can I map each Employee with the Employee ID value?

XML File:

<Workers>
    <Employee>
            <Employee_Summary>
                <Employee_ID>1</Employee_ID>
                <Name>Name </Name>
                <Company_Code>Company Code</Company_Code>
                <Unit_Code>Unit Code</Unit_Code>
            </Employee_Summary>
            <Company_Summary>
                <Company_Name> Company Name</Company_Name>
                <Company_Adress> Company Adress </Company_Adress>
                <Company_Status> Company Status <Company_Status>
            </Company_Summary>
    </Employee>

    <Employee>
            <Employee_Summary>
                <Employee_ID>2</Employee_ID>
                <Name>Name </Name>
                <Company_Code>Company Code</Company_Code>
                <Unit_Code>Unit Code</Unit_Code>
            </Employee_Summary>
            <Company_Summary>
                <Company_Name> Company Name</Company_Name>
            </Company_Summary>
    </Employee>


    <Employee>
            <Employee_Summary>
                <Employee_ID>3</Employee_ID>
                <Name>Name </Name>
                <Unit_Code>Unit Code</Unit_Code>
            </Employee_Summary>
            <Company_Summary>
                <Company_Name> Company Name</Company_Name>
                <Company_Adress> Company Adress </Company_Adress>
                <Company_Status> Company Status <Company_Status>
            </Company_Summary>
    </Employee>
</Workers>
  • What's a "sub-node"? Is `Name` a sub-node? What about `Company_Summary` and `Company_Name`? – canton7 Feb 11 '20 at 14:02
  • Yes, everything under the Employee tag is considered as a sub node. – chapmanuno Feb 11 '20 at 14:03
  • So there are 9 sub-nodes for the Employee with ID 1? (`Employee_Summary`, `Employee_ID`, `Name`, `Company_Code`, `Unit_Code`, `Company_Summary`, `Company_Name`, `Company_Adress`, `Company_Status`) – canton7 Feb 11 '20 at 14:03
  • Yes, thats correct! – chapmanuno Feb 11 '20 at 14:04
  • Does this answer your question? [Getting the count of xml elements under an XML Node](https://stackoverflow.com/questions/29481638/getting-the-count-of-xml-elements-under-an-xml-node) – Jimmy Smith Feb 11 '20 at 14:12

1 Answers1

2

You can do this easily with XDocument:

var doc = XDocument.Parse(xml);
var results = new Dictionary<int, int>();
foreach (var employee in doc.Root.Elements("Employee"))
{
    int id = (int)employee.Element("Employee_Summary").Element("Employee_ID");
    int count = employee.Descendants().Count();
    results[id] = count;
}

You can also use XmlDocument:

var doc = new XmlDocument();
doc.LoadXml(xml);
var results = new Dictionary<int, int>();
foreach (XmlNode employee in doc.SelectNodes("/Workers/Employee"))
{
    int id = int.Parse(employee.SelectSingleNode("./Employee_Summary/Employee_ID").InnerText);
    int count = employee.SelectNodes(".//*").Count;
    results[id] = count;
}
canton7
  • 37,633
  • 3
  • 64
  • 77