Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据特定的父ID LINQ获取XML项_C#_Asp.net_Xml_Linq_Linq To Xml - Fatal编程技术网

C# 根据特定的父ID LINQ获取XML项

C# 根据特定的父ID LINQ获取XML项,c#,asp.net,xml,linq,linq-to-xml,C#,Asp.net,Xml,Linq,Linq To Xml,我有以下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</Dat

我有以下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>
我如何让它根据GroupID实际获得产品

谢谢你的帮助和时间

*****************更新***********************


将节点强制转换为
int
string
DateTime
,而不是转换节点值:

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"),
   PersonId = (int)g.Element("PersonId"),
   DateCreated = (DateTime)g.Element("DateCreated"),
   DateModified = (DateTime)g.Element("DateModified"),
   Products = g.Elements("Products")
               .Select(p => new Product {                   
                   ProductId = (int)p.Attribute("ProductId"),
                   ProductName = (string)p.Element("ProductName"),
                   CategoryName = (string)p.Element("CategoryName"),
                   SubCategoryId = (int)p.Element("SubCategoryId"),
                   SubCategoryName = (string)p.Element("SubCategoryName")
               }).ToList()
}

另外,我认为最好将xml中的所有产品分组在
元素下,每个产品都包含
元素。

使用节点转换为
int
字符串
、或
日期时间
,而不是转换节点值:

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"),
   PersonId = (int)g.Element("PersonId"),
   DateCreated = (DateTime)g.Element("DateCreated"),
   DateModified = (DateTime)g.Element("DateModified"),
   Products = g.Elements("Products")
               .Select(p => new Product {                   
                   ProductId = (int)p.Attribute("ProductId"),
                   ProductName = (string)p.Element("ProductName"),
                   CategoryName = (string)p.Element("CategoryName"),
                   SubCategoryId = (int)p.Element("SubCategoryId"),
                   SubCategoryName = (string)p.Element("SubCategoryName")
               }).ToList()
}

另外,我认为最好将xml中的所有产品分组在
元素下,每个产品都有
元素。

什么是xdoc.substands(“组”)?你是说xdoc.后代(“组”)?什么是xdoc.后代(“组”)?你是说xdoc.后代(“组”)?是的,这是有效的。汉克斯·别列佐夫斯基。为什么所有的产品都在一个元素下?我认为他们会更好地分组。我发现的问题是,它只返回一个产品,即使有多个产品。我用新代码更新了问题对不起,别理我的评论别列佐夫斯基,只是注意到了硬编码bit@Johann因为你有一组产品,xml是分层的,所以把产品分组在一些分组节点下是很好的。好吧,我知道你知道别列佐夫斯基,我添加了产品标签来包装产品。是的,这很有效。汉克斯·别列佐夫斯基。为什么所有的产品都在一个元素下?我认为他们会更好地分组。我发现的问题是,它只返回一个产品,即使有多个产品。我用新代码更新了问题对不起,别理我的评论别列佐夫斯基,只是注意到了硬编码bit@Johann因为您有一组产品,xml是分层的,所以最好将产品分组到某个分组节点下。好的,我知道您知道别列佐夫斯基,我添加了产品标签来包装产品
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"),
   PersonId = (int)g.Element("PersonId"),
   DateCreated = (DateTime)g.Element("DateCreated"),
   DateModified = (DateTime)g.Element("DateModified"),
   Products = g.Elements("Products")
               .Select(p => new Product {                   
                   ProductId = (int)p.Attribute("ProductId"),
                   ProductName = (string)p.Element("ProductName"),
                   CategoryName = (string)p.Element("CategoryName"),
                   SubCategoryId = (int)p.Element("SubCategoryId"),
                   SubCategoryName = (string)p.Element("SubCategoryName")
               }).ToList()
}