Tutorial :Count specific XML Nodes within XML



Question:

See this XML:


<CMP>      <OMP3>          <personmenu>              <submenuid>502</submenuid>              <submenuid>503</submenuid>          </personmenu>          <accountsmenu>              <submenuid>517</submenuid>              <submenuid>518</submenuid>              <submenuid>519</submenuid>          </accountsmenu>            <reportsmenu>              <submenuid>522</submenuid>              <submenuid>528</submenuid>              <submenuid>536</submenuid>          </reportsmenu>      </OMP3>        <AMP3>          <admissionmenu>              <submenuid>702</submenuid>              <submenuid>703</submenuid>          </admissionmenu>      </AMP3>  </CMP>  

I want to get the total count of nodes from this xml dynamically (C#). How can I do it? Any sample code?


Solution:1

Here's an example of counting all submenuid nodes in your xml document without loading it into memory:

var nodeCount = 0;  using (var reader = XmlReader.Create("test.xml"))  {      while (reader.Read())      {          if (reader.NodeType == XmlNodeType.Element &&               reader.Name == "submenuid")          {              nodeCount++;          }      }  }  Console.WriteLine(nodeCount);  

Or if you prefer LINQ to XML:

var count = XDocument      .Load("test.xml")      .XPathSelectElements("//submenuid")      .Count();  


Solution:2

Use XmlDocument like this:

XmlDocument xmlD = new XmlDocument();  xmlD.Load(Server.MapPath("sample.xml"));  XmlNodeList xmlNL = xmlD.GetElementsByTagName("tagName");  xmlNL.Count;  


Solution:3

You can use XPath function "count" as well. here is a example.

XPathDocument doc = new XPathDocument("c:\\test.xml");  int count = (int)doc.CreateNavigator().Evaluate("count(//submenuid)");  


Solution:4

Below code is to find the count of a specific node in the XML Document

 private void browse_Click(object sender, EventArgs e)//file browse button      {          DialogResult result = openFileDialog1.ShowDialog();          if (result == DialogResult.OK)          {              String file = openFileDialog1.FileName;              if (Path.GetExtension(file) != ".xml")              {                  MessageBox.Show("Please upload an vaild xml file");                  textBox1.Clear();              }              else              {                  textBox1.Text = file;              }          }      }     private void CountButton_Click(object sender, EventArgs e)//count button      {          int count = 0;          string element = textBox2.Text;//Enter the node in the textbox          XmlDocument readdoc = new XmlDocument();              readdoc.Load(textBox1.Text);              XmlElement root = readdoc.DocumentElement;              XmlNodeList node = root.GetElementsByTagName(element);              count = node.Count;              MessageBox.Show(string.Format("Count of {0} node in the uploaded xml file is {1}", element, count.ToString()));      }  


Solution:5

If you have control of your xml

string xmlReturn="  <CMP>  <OMP3>      <personmenu>          <submenuid>502</submenuid>          <submenuid>503</submenuid>      </personmenu>      <accountsmenu>          <submenuid>517</submenuid>          <submenuid>518</submenuid>          <submenuid>519</submenuid>      </accountsmenu>        <reportsmenu>          <submenuid>522</submenuid>          <submenuid>528</submenuid>          <submenuid>536</submenuid>      </reportsmenu>  </OMP3>    <AMP3>      <admissionmenu>          <submenuid>702</submenuid>          <submenuid>703</submenuid>      </admissionmenu>  </AMP3>  </CMP>"  XmlDocument xmldoc = new XmlDocument();  xmldoc.LoadXml(xmlReturn);  XmlNodeList nodeListCount=xmldoc.GetElementsByTagName("submenuid");  int nodeCount = nodeListCount.Count;  Console.WriteLine(nodeCount);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »