locked
count node xml file, RRS feed

  • Question

  • User289883540 posted

    hi all.

    I want someone advise me how can I count the nodes in xml file

    I have this xml file

    <?xml version="1.0" encoding="utf-8"?>

    <!--Programmatically writing XML-->

    <Employees>

      <Employee type="Permanent">

        <ID>A</ID>

        <FirstName>Satheesh</FirstName>

        < LastName>&lt;b&gt;Babu</LastName>

        <Dept>IT</Dept>

      </Employee>

      <Employee type="Temporary">

        <ID>B</ID>

        <FirstName>Peters</FirstName>

        <LastName>Laurence</LastName>

        <Dept>Development</Dept>

      </Employee>

      <Employee type="Permamnet">

        <ID>C</ID>

        <FirstName>Rick</FirstName>

        <LastName>Anderson</LastName>

        <Dept>Sales</Dept>

      </Employee>

      <Employee type="Temporary">

        <ID>D</ID>

        <FirstName>Ramesh</FirstName>

        <LastName>Kumar</LastName>

        <Dept>HR</Dept>

      </Employee>

    </Employees>

    when I upload the xml file and press submitbtn I want count the nodes and put it in gridview

    for example, I have

    Employee type = 4

    ID = 4

    Saturday, March 5, 2016 1:10 PM

Answers

  • User2103319870 posted

    micheal85

    could you provide me the code please

    You can use  XMLDocument  node count method to get the count .

    Try with the below code

     XmlDocument readDoc = new XmlDocument();
                    readDoc.Load(Server.MapPath("XMLFile2.xml"));
                    //Get Employee Count
                    int Employeecount = readDoc.SelectNodes("Employees/Employee").Count;
                    //Get EmployeeID Count
                    int idcount = readDoc.SelectNodes("Employees/Employee/ID").Count;

    You also need to add XmlDocument namespace

    using System.Xml;
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 5, 2016 3:13 PM

All replies

  • User1578460427 posted

    You can use the XmlNodeList.Count property in the System.Xml namespace to count the nodes in the XML file.

    Saturday, March 5, 2016 1:37 PM
  • User289883540 posted

    could you provide me the code please

    Saturday, March 5, 2016 2:57 PM
  • User2103319870 posted

    micheal85

    could you provide me the code please

    You can use  XMLDocument  node count method to get the count .

    Try with the below code

     XmlDocument readDoc = new XmlDocument();
                    readDoc.Load(Server.MapPath("XMLFile2.xml"));
                    //Get Employee Count
                    int Employeecount = readDoc.SelectNodes("Employees/Employee").Count;
                    //Get EmployeeID Count
                    int idcount = readDoc.SelectNodes("Employees/Employee/ID").Count;

    You also need to add XmlDocument namespace

    using System.Xml;
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 5, 2016 3:13 PM
  • User-595703101 posted

    Hi Michael,

    If you want to use T-SQL, following code can help

    declare @xml xml = '
    <Employees>
      <Employee type="Permanent">
        <ID>A</ID>
        <FirstName>Satheesh</FirstName>
    	<LastName>Babu</LastName>
        <Dept>IT</Dept>
      </Employee>
      <Employee type="Temporary">
        <ID>B</ID>
        <FirstName>Peters</FirstName>
        <LastName>Laurence</LastName>
        <Dept>Development</Dept>
      </Employee>
      <Employee type="Permamnet">
        <ID>C</ID>
        <FirstName>Rick</FirstName>
        <LastName>Anderson</LastName>
        <Dept>Sales</Dept>
      </Employee>
      <Employee type="Temporary">
        <ID>D</ID>
        <FirstName>Ramesh</FirstName>
        <LastName>Kumar</LastName>
        <Dept>HR</Dept>
      </Employee>
    </Employees>'
    
    ;with cte as (
    	SELECT
    	 e.value('.','varchar(100)') as empID
    	FROM @xml.nodes('/Employees/Employee') as employee(e)
    )
    select count(*) employee_count from cte
    

    For more samples on SQL XML to query XML on SQL Server please check the referred tutorial

    Monday, March 7, 2016 7:47 AM
  • User36583972 posted

    Hi micheal85,

    From your description, you can use Linq to XML. I have made a sample on my side. The following code in my project for your reference.

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <Employees>
      <Employee type="Permanent">
        <ID>A</ID>
        <FirstName>Satheesh</FirstName>
        <LastName>&lt;b&gt;Babu</LastName>
        <Dept>IT</Dept>
      </Employee>
      <Employee type="Temporary">
        <ID>B</ID>
        <FirstName>Peters</FirstName>
        <LastName>Laurence</LastName>
        <Dept>Development</Dept>
      </Employee>
      <Employee type="Permamnet">
        <ID>C</ID>
        <FirstName>Rick</FirstName>
        <LastName>Anderson</LastName>
        <Dept>Sales</Dept>
      </Employee>
      <Employee type="Temporary">
        <ID>D</ID>
        <FirstName>Ramesh</FirstName>
        <LastName>Kumar</LastName>
        <Dept>HR</Dept>
      </Employee>
    </Employees>
    

    aspx.cs:

     protected void Button2_Click(object sender, EventArgs e)
            {
                string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase+ "test.xml";
                XElement xe = XElement.Load(str);
                IEnumerable<XElement> elements = from ele in xe.Elements("Employee")
                                                 select ele;
                showInfoByElements(elements);
            }
    
    
            private void showInfoByElements(IEnumerable<XElement> elements)
            {
                List<Employee> modelList = new List<Employee>();
                foreach (var ele in elements)
                {
                    Employee model = new Employee();
                    model.type = ele.Attribute("type").Value;
                    model.ID = ele.Element("ID").Value;
                    model.FirstName = ele.Element("FirstName").Value;
                    model.LastName = ele.Element("LastName").Value;
                    model.Dept = ele.Element("Dept").Value;
                    modelList.Add(model);
                }
                GridView1.DataSource = modelList;
                GridView1.DataBind();
                Response.Write("Count Nodes:"+modelList.Count.ToString());
            }

    Icon:

    Best Regards,

    Yohann Lu

    Tuesday, March 8, 2016 9:19 AM