Have a look at example:
//define xml document content
string xcontent = @"='1.0'
<table>
<rows>
<row number='0'>
<col number='1' header='true'>A</col>
<col number='2' header='true'>B</col>
</row>
<row number='1'>
<col number='1' header='false'>ValueA1</col>
<col number='2' header='false'>ValueB1</col>
</row>
<row number='2'>
<col number='1' header='false'>ValueA2</col>
<col number='2' header='false'>ValueB2</col>
</row>
</rows>
</table>";
//create xml document from string
XDocument xdoc = XDocument.Parse(xcontent);
//get the name of element, its level and no. of subnodes
var result = xdoc.Descendants()
.Select(x=>new
{
NodeName = x.Name,
NodeLevel = x.AncestorsAndSelf().Count(),
ChildsCount = x.Descendants().Count()
}
)
.Distinct();
Result:
NodeName NodeLevel ChildsCount
table 1 10
rows 2 9
row 3 2
col 4 0
You might be interested in other properties and methods of
XmlElement class[
^]. A most interesting is
XElement.AncestorsAndSelf Method ()[
^]