I am reading an XML file using XDocument and loading it into classes. Each “site” node should be a class with the “tags” node being a List within it. I am having trouble loading the “tags” elements into the class as a List.
Here are the classes:
public class Site
{
public string name;
public string prefix;
public List<Tag> tags;
}
public class Tag
{
public string Name { get; set; }
public string Column { get; set; }
}
This is the code that is loading XML data into the classes:
settings = XDocument.Load("settings.xml");
IEnumerable<Site> sites = settings.Descendants("site").Select(x => new Site()
{
name = x.Element("name").Value,
prefix = x.Element("prefix").Value,
// How to load the "tag" elements into a List<Class>?
});
Using the following XML example:
<settings>
<site>
<name>name1</name>
<prefix>prefix1</prefix>
<tags>
<tag column="col1">tag1</tag>
<tag column="col2">tag2</tag>
<tag column="col3">tag3</tag>
</tags>
</site>
<site>
<name>name2</name>
<prefix>prefix2</prefix>
<tags>
<tag column="col1">tag1</tag>
<tag column="col2">tag2</tag>
<tag column="col3">tag3</tag>
</tags>
</site>
</settings>
Thanks.
Try this, at the location of your comment // How to load… : (untested)