Your XML doesn't show the Cities, as you said this was the first parent-child view of it.
Secondly, each pair should be enclosed in a tag, such as <Table>
and probably each pair if they have similar items in a <Parent>
and <Child>
tags respectfully.
Then if the internal tags are the same throughout, you can use the same class to get the information, making putting it into your form straightforward.
Such as:
Table[] tables = XElement.Load(file)
.Elements("Table")
.Select(table => new Table(table))
.ToArray();
Each Table then would have a Parent and a Child of a class of whatever you want to call it to read its data. They should be the same class if they have the same elements, otherwise you will need two different classes.
A Table would be minimally:
public class Table
{
public readonly XElement element;
public Table(XElement element){ this.element = element; }
public int Id { get { return (int)element.Attribute("Id"); } }
public MyPair Parent
{ get { return _Parent ?? (_Parent = new MyPair(element.Element("Parent"))); } }
MyPair _Parent;
public MyPair Child
{ get { return _Child ?? (_Child = new MyPair(element.Element("Child"))); } }
MyPair _Child;
}
A MyPair
would be something like:
public class MyPair
{
public readonly XElement element;
public MyPair(XElement element){ this.element = element; }
public string[] Adapters
{
get
{
return _Adapters ?? (_Adapters = element.Descendants("CommandText")
.Select(ct => (string)ct)
.ToArray());
}
}
string[] _Adapters;
public string[] Members
{
get
{
return _Members ?? (_Members = element.Descendants("DataMember")
.Select(dm => (string)dm)
.ToArray());
}
}
string[] _Members;
}