私は次のXMLを持っています
<Group>
<Id>1</Id>
<GroupName>Fruit Juices</GroupName>
<PersonId>46</PersonId>
<DateCreated>0001-01-01T00:00:00</DateCreated>
<DateModified>0001-01-01T00:00:00</DateModified>
<Products ProductId="24">
<ProductName>Grapes</ProductName>
<CategoryId>2</CategoryId>
<CategoryName></CategoryName>
<SubCategoryId>4</SubCategoryId>
<SubCategoryName>Armbands</SubCategoryName>
</Products>
</Group>
<Group>
<Id>2</Id>
<GroupName>Wrist Watches</GroupName>
<PersonId>49</PersonId>
<DateCreated>0001-01-01T00:00:00</DateCreated>
<DateModified>0001-01-01T00:00:00</DateModified>
<Products ProductId="20">
<ProductName>Hard Drives</ProductName>
<CategoryId>2</CategoryId>
<CategoryName></CategoryName>
<SubCategoryId>4</SubCategoryId>
<SubCategoryName>Armbands</SubCategoryName>
</Products>
<Products ProductId="24">
<ProductName>Grapes</ProductName>
<CategoryId>2</CategoryId>
<CategoryName></CategoryName>
<SubCategoryId>4</SubCategoryId>
<SubCategoryName>Armbands</SubCategoryName>
</Products>
<Products ProductId="34">
<ProductName>Oranges</ProductName>
<CategoryId>2</CategoryId>
<CategoryName></CategoryName>
<SubCategoryId>4</SubCategoryId>
<SubCategoryName>Armbands</SubCategoryName>
</Products>
<Products ProductId="50">
<ProductName>Printers</ProductName>
<CategoryId>2</CategoryId>
<CategoryName></CategoryName>
<SubCategoryId>4</SubCategoryId>
<SubCategoryName>Armbands</SubCategoryName>
</Products>
<Products ProductId="52">
<ProductName>Printers</ProductName>
<CategoryId>2</CategoryId>
<CategoryName></CategoryName>
<SubCategoryId>4</SubCategoryId>
<SubCategoryName>Armbands</SubCategoryName>
</Products>
</Group>
今の私の目標は、特定のグループIDのすべての製品を取得することです。
私は以下を試しましたが、機能していません:-
foreach (XElement xe in xdoc.Descendants("Group"))
{
Product product = new Product();
Group group = new Group();
group.Id = Convert.ToInt32(xe.Element("Id").Value);
group.GroupName = xe.Element("GroupName").Value;
group.PersonId = Convert.ToInt32(xe.Element("PersonId").Value);
group.DateCreated = Convert.ToDateTime(xe.Element("DateCreated").Value);
group.DateModified = Convert.ToDateTime(xe.Element("DateModified").Value);
var products = xdoc.Descendants("Groups")
.Where(node => (string)node.Attribute("Id") == group.Id.ToString())
.GroupBy(x => x.Value)
.Select(x => new { Name = x.Key, Count = x.Count() })
.ToList();
GroupList.Add(group);
}
GroupIDに従って実際に製品を取得するにはどうすればよいですか?
あなたの助けと時間をありがとう
** * ** * ** * ** * ******更新**** ** * ** * ** * ** * ** * _ _ _ _
foreach (XElement xe in xdoc.Descendants("Group"))
{
var group = from g in xdoc.Descendants("Group")
where (int)g.Element("Id") == 1 // filtering groups here
select new Group
{
Id = (int)g.Element("Id"),
GroupName = (string)g.Element("GroupName"),
PersonId = (int)g.Element("PersonId"),
DateCreated = (DateTime)g.Element("DateCreated"),
DateModified = (DateTime)g.Element("DateModified"),
Products = g.Elements("Products")
.Select(p => new Product
{
Id = (int)p.Attribute("ProductId"),
ProductName = (string)p.Element("ProductName")
}).ToList()
};
foreach (var g in group)
{
GroupList.Add(g);
}
}