我有以下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>
我希望在Group对象中检索以下内容。
我有以下代码: -
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);
}
}
但是我收到错误,因为Person不是IEnumerable。但是因为每组只有1个人,所以我不想让它成为IEnumerable。有办法解决这个问题吗?
感谢您的帮助和时间
答案 0 :(得分:0)
最好声明新的范围变量,该变量将保留<Person>
元素并在以后使用
var group = from g in xdoc.Descendants("Group")
let person = g.Element("Person") // here
where (int)g.Element("Id") == id
select new Group {
...
}
并使用它:
Person = new Person {
Id = (int)person.Element("PersonId"),
Name = (string)person.Element("PersonName"),
Surname = (string)person.Element("PersonSurname"),
Age = (int)person.Element("PersonAge")
},
您还可以添加null
检查xml中是否可能没有person元素:
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")
},
答案 1 :(得分:0)
听起来你可以做:
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()
但最好写一下:
Person = Person.FromXElement(g.Element("Person"))
其中FromXElement
是Person
中的静态方法,如下所示:
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")
};
}
这样你的查询就会更加清晰。