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

时间:2022-09-11 20:29:26

I have the following 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</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>

Now my aim is to get all the products for a specific Group ID.

现在我的目标是获得特定组ID的所有产品。

I tried the following but it is not working :-

我尝试了以下但是它不起作用: -

            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);
        }

How can I get it working to actually get the Products according to the GroupID?

我怎样才能让它根据GroupID实际获得产品?

Thanks for your help and time

谢谢你的帮助和时间

******************UPDATE*******************

****************** UPDATE *******************

            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);
            }

        }

1 个解决方案

#1


1  

Use casting of nodes to int, string, or DateTime instead of converting node value:

使用节点转换为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()
}

Also I think it's better to group all products in your xml under <Products> element with <Product> elements for each product.

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

#1


1  

Use casting of nodes to int, string, or DateTime instead of converting node value:

使用节点转换为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()
}

Also I think it's better to group all products in your xml under <Products> element with <Product> elements for each product.

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