Hello all. I have an XML file with property groups and properties. I want to retrieve the property groups, print the Name attribute from that element, and then print the child properties of said property group below that entry.
Example XML file:
<?xml version="1.0" ?>
<XMLDocument xmlns:ns="http://www.namespace.com/ns">
<ns:collectionProperties>
<ns:propertyGroup name="PERSON">
<ns:property key="ID" value="555"/>
<ns:property key="Name" value="Ehron"/>
<ns:property key="Location" value="Atlanta"/>
</ns:propertyGroup>
<ns:propertyGroup name="DOG">
<ns:property key="Dog Name" value="Lenny"/>
<ns:property key="Dog Breed" value="Corgle"/>
</ns:propertyGroup>
<ns:propertyGroup name="CAT">
<ns:property key="Cat Color" value="Grey"/>
<ns:property key="Cat Hates" value="Everyone"/>
<ns:property key="Name" value="Lester"/>
</ns:propertyGroup>
</ns:collectionProperties>
</XMLDocument>
Desired resultant text:
[PERSON]
ID=555
Name=Ehron
Location=Atlanta
[DOG]
Dog Name=Lenny
Dog Breed=Corgle
[CAT]
Cat Color=Grey
Cat Hates=Everyone
Name=Lester
I managed to get the propertyGroup name to print, but I can't seem to retrieve more than one attribute from the property elements without doing two LINQ queries. I have not found a way to do this thus far.
static void Main(string[] args)
{
XDocument xml = XDocument.Load(@"c:\xml.xml");
XNamespace ns = "http://namespace.com/ns";
// Pull out the categories
var categories = from c in xml.Descendants(ns + "propertyGroup")
select (string)c.Attribute("name");
// write categories
foreach (string name in categories)
{
Console.WriteLine('[' + name + ']');
}
}