I need to read all the elements in an XML file that has a format which is more of a tree-like hierarchy, and then populate a class with it here is a sample:
<?xml version="1.0"?>
<WBs>
<WP GeneralID ="1">
<P name="General_Header">
<Q name= "Category">
<Tools>
<Tool id ="1">
<TName> QT 1</TName>
<Rev>1</Rev>
</Tool>
<Tool id ="2">
<TName> QT 2</TName>
<Rev>3</Rev>
</Tool>
</Tools>
<Contacts>
<Contact>
<CName>MM</CName>
<CMail>m.m@i.com</CMai>
</Contact>
<Contact>
<CName>AM</CName>
<CMail>a.m@i.com</CMail>
</Contact>
</Contacts>
</Q>
<ss name= "Category">
<Tools>
<Tool id ="1">
<TName> SST 1</TName>
<Rev>3</Rev>
</Tool>
<Tool id ="2">
<TName> SST 2</TName>
<Rev>3</Rev>
</Tool>
</Tools>
<Contacts>
<Contact>
<CName>KE</CName>
<CMail>K.E@i.com</CMai>
</Contact>
<Contact>
<CName>AM</CName>
<CMail>a.m@i.com</CMail>
</Contact>
</Contacts>
</ss>
</P>
</WP>
</WBs>
I have made a class for each of the WP, tool, contact. as follows:
class WP
{
public string GeneralHeader { get; set; } //level 1 i.e p,
public string Category { get; set; }// level 2
public string SubCategory { get; set; }//level 3
public string SubSubCategory { get; set; } // level 4
public List<Tool> WPTools { get; set; }
public List<Contact> WPContacts { get; set; }
}
What I want to do is to traverse through all of the element, and child elements and then populate the WP Class such that whenever I encounter two child elements this should be in two different Objects of the WP, but has the same parent attribute.
For example: For the above sample i was hoping to get two objects from the WP Class, withe same "General_Header" parameter as "P", but one object is having the "Category" equal to "Q" and the other is having "SS", then continue to populate each one with the corresponding tools and contacts. the idea is the same as the rest of the XML file have the same issue at different levels for example: WP Tags is having branches in the "SubCategory", and others in the "SubSubCategory".
All that I can think of is to change the xml file so that every complete branch (till the tools and contacts tags) is included in a separate set of <WP>---</WP>
tags, but in this case I would repeat the common parent tags, which I don't think is an efficient way of using xml.
Any suggestions?
Thanks in advance.