Count number of elements and attributes using LINQ

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="color:blue Hello all,
<span style="color:blue I am a beginner in LINQ and need help. I am trying to count all elements and attributes (excluding duplicates) in an XML file. For example,
<span style="color:blue
<pre><?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document audience = "novice
<heading>Chapter 1</heading>
<paragraph audience="expert This is paragraph 1</paragraph>
<paragraph>This is paragraph 2</paragraph>
<section platform="os
<paragraph/>
</section>
</document>
[/code]
Here, I need the Element count: 4 and Attribute count: 2 (after excluding duplicate names).
How can I achieve this? I have gotten so far:
<div style="color:Black;background-color:White; <pre>
XDocument xDocument = XDocument.Load(xmlFilePath);
IEnumerable<XElement> elementList = xmlDocument.Root.Descendants().Distinct();
<span style="color:Blue; int count = elementList.Count();




[/code]
But this includes all the descendants of the root element including duplicate names...
Many thanks,
Greets<br/>
<br/>




View the full article
 
Back
Top