How do I differentiate between different descendents with the same name?
Posted
by zotty
on Stack Overflow
See other posts from Stack Overflow
or by zotty
Published on 2010-03-19T12:51:54Z
Indexed on
2010/03/19
13:01 UTC
Read the original article
Hit count: 208
I've got some XML I'm trying to import with c#, which looks something like this:
<run>
<name = "bob"/>
<date = "1958"/>
</run>
<run>
<name = "alice"/>
<date = "1969"/>
</run>
I load my xml using
XElement xDoc=XElement.Load(filename);
What I want to do is have a class for "run", under which I can store names and dates:
public class RunDetails
{
public RunDetails(XElement xDoc, XNamespace xmlns)
{
var query = from c in xDoc.Descendants(xmlns + "run").Descendants(xmlns + "name") select c;
int i=0;
foreach (XElement a in query)
{
this.name= new NameStr(a, xmlns); // a class for names
Name.Add(this.name); //Name is a List<NameStr>
i++;
}
// Here, i=2, but what I want is a new instance of the RunDetails class for each <run>
}
}
How can I set up my code to create a new instance of the RunDetails class for every < run>, and to only select the < name> and < date> inside a given < run>?
© Stack Overflow or respective owner