Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Answers


Following example explains in detail How to count child nodes

XML:

<?xml version="1.0" encoding="UTF-8"?>
<
Collage>
  <
Students branch="IT">
    <
Student>Shourya</Student>
    <
Year>2014</Year>
    <
teacher>Nagnath</teacher>
  </
Students>
  <
Students branch="IT">
    <
Student>Shrad</Student>
    <
Year>2015</Year>
    <
teacher>Girish</teacher>
  </
Students>
 
  <
Students branch="Mechanical">
    <
Student>aditya</Student>
    <
Year>2015</Year>
    <
teacher>Balaji</teacher>
  </
Students>
  </
Collage>

Linq to XML Query:

var doc = XDocument.Load("~/nmk/students.xml");
int descendantsNodes =doc.Descendants().Count(); // counts ALL descendants elements
int childNodes =doc.Root.Elements().Count(); //counts direct children of the root element

Share your answer


Share as a guest

Please subscribe me to the CodeChef4U.

By creating your new answer, you agree to the privacy policy and terms of service.