I have an XML file that has several tags like this:
<sitecollection name="">
<site name="">
<maingroup name="">
<group name=""> </group>
</maingroup>
</site>
<sitecollection>
The idea is to loop through all the sitecollection
and it's child elements in the XML document, and save the info in variables. The problem I'm having is saving the child elements, with their attributes.
So far I have the following code:
class xmlreader
{
public static void Main()
{
XDocument xdoc = XDocument.Load("xmldocument.xml");
var result = new System.Text.StringBuilder();
var lv1s = from lv1 in xdoc.Descendants("sitecollection")
select new
{
siecollection = lv1.Attribute("name").Value,
maingroup = lv1.Descendants("group")
};
foreach (var lv1 in lv1s)
{
result.AppendLine(lv1.siecollection);
foreach (var lv2 in lv1.maingroup)
result.AppendLine(" " + lv2.Attribute("name").Value);
}
}
}