Question

Is there a good way in c# to look through an XML node list using DOM and get a node list of only the unique nodes and also a list of each nodes unique possible attributes.

The XMl file in question has nodes of the same name but with different attributes, i want a list of all the possible ones. Also the list of nodes i would like to be only of the unique nodes, rather than having repeats (so node lists i generate at the moment might have contact twice, three time ect within it). And it needs to work for any XML document. Any ideas?

Here is an example:

 <book id="bk112">
  <author>Galos, Mike</author> 
  <title>Visual Studio 7: A Comprehensive Guide</title> 
  <genre>Computer</genre> 
  <price>49.95</price> 
  <publish_date>2001-04-16</publish_date> 
 </book>
 <book id="bk162">
  <genre>fiction</genre> 
  <popularity>High</popularity> 
  <price>20.00</price> 
  <publish_date>2002-03-12</publish_date> 
 </book>
 <cd id="bk162">
  <genre>jaz</genre> 
  <popularity>High</popularity> 
  <price>10.00</price>
 </cd>

and get some sort of output like:

there are 2 of the type book
there are 1 of the type cd
there are 3 of the type genre 
book may have the attributes author, title, genre, price, popularity, publish_date

but in a way that works for any xml file.

In the case of genre it doesnt need to be celver in any way, just know there are 3 genre nodes in the document.

Was it helpful?

Solution

Would this do it?

XDocument xDoc = XDocument.Load("XMLFile1.xml");
List<XElement> distinctDocs = xDoc.Descendants().GroupBy(x => x.Name).Where(x => x.Count() == 1).Select(g => g.Single()).ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top