I have the following XML
<Group>
<Id>2</Id>
<GroupName>Fizzy Drinks</GroupName>
<DateCreated>0001-01-01T00:00:00</DateCreated>
<DateModified>0001-01-01T00:00:00</DateModified>
<Person>
<PersonId>78</PersonId>
<PersonName>Francesca</PersonName>
<PersonSurname>Andrews</PersonSurname>
<PersonAge>59</PersonAge>
</Person>
<Products>
<ProductId>2</ProductId>
<ProductName>Oranges</ProductName>
<CategoryId>4</CategoryId>
<CategoryName></CategoryName>
<SubCategoryId>7</SubCategoryId>
<SubCategoryName>Bread</SubCategoryName>
</Products>
<Products>
<ProductId>12</ProductId>
<ProductName>Pepsi</ProductName>
<CategoryId>4</CategoryId>
<CategoryName></CategoryName>
<SubCategoryId>8</SubCategoryId>
<SubCategoryName>Dairy</SubCategoryName>
</Products>
<Products>
<ProductId>14</ProductId>
<ProductName>Multiwheat Bread</ProductName>
<CategoryId>4</CategoryId>
<CategoryName></CategoryName>
<SubCategoryId>7</SubCategoryId>
<SubCategoryName>Bread</SubCategoryName>
</Products>
</Group>
And I wish to retreive the following inside a Group object.
I have the following code :-
foreach (XElement xe in xdoc.Descendants("Group"))
{
int id = Convert.ToInt32(xe.Element("Id").Value);
var group = from g in xdoc.Descendants("Group")
where (int)g.Element("Id") == id // filtering groups here
select new Group
{
Id = (int)g.Element("Id"),
GroupName = (string)g.Element("GroupName"),
DateCreated = (DateTime)g.Element("DateCreated"),
DateModified = (DateTime)g.Element("DateModified"),
Person = from d in g.Descendants("Person")
select new Person
{
Id = (int)d.Element("PersonId"),
Name = (string)d.Element("PersonName"),
Surname = (string)d.Element("PersonSurname"),
Age = (int)d.Element("PersonAge"),
},
PersonId = (int)g.Element("PersonId"),
PersonName = (string)g.Element("PersonName"),
PersonSurname = (string)g.Element("PersonSurname"),
PersonAge = (int)g.Element("PersonAge"),
Products = g.Elements("Products")
.Select(p => new Product
{
Id = (int)p.Element("ProductId"),
ProductName = (string)p.Element("ProductName")
}).ToList()
};
foreach (var g in group)
{
GroupList.Add(g);
}
}
However I am getting an error since the Person is not IEnumerable. However since I will only ever have 1 person per group, I do not wish to make it IEnumerable. Is there a way around this?
Thanks for your help and time
It's better do declare new range variable which will hold <Person>
element and use it later
var group = from g in xdoc.Descendants("Group")
let person = g.Element("Person") // here
where (int)g.Element("Id") == id
select new Group {
...
}
And use it:
Person = new Person {
Id = (int)person.Element("PersonId"),
Name = (string)person.Element("PersonName"),
Surname = (string)person.Element("PersonSurname"),
Age = (int)person.Element("PersonAge")
},
You can also add null
check if it is possible that there is no person element in your xml:
Person = (person == null) ? null :
new Person {
Id = (int)person.Element("PersonId"),
Name = (string)person.Element("PersonName"),
Surname = (string)person.Element("PersonSurname"),
Age = (int)person.Element("PersonAge")
},
It sounds like you could do:
Person = (from d in g.Elements("Person")
select new Person
{
Id = (int)d.Element("PersonId"),
Name = (string)d.Element("PersonName"),
Surname = (string)d.Element("PersonSurname"),
Age = (int)d.Element("PersonAge"),
}).First()
but it would be better to write:
Person = Person.FromXElement(g.Element("Person"))
where FromXElement
is a static method in Person
like this:
public static Person FromXElement(XElement element)
{
return new Person
{
Id = (int) element.Element("PersonId"),
Name = (string) element.Element("PersonName"),
Surname = (string) element.Element("PersonSurname"),
Age = (int) element.Element("PersonAge")
};
}
That way your query will be a lot clearer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.