1

My xml data:

<products>
  <group groupcontractno="106" groupcontractversion="40">
      <product groupcontractno="106" groupcontractversion="40" cobno="1" productno="100" cobproductversion="4">
        <productname>Båtförsäkring</productname>
      </product>
      <product groupcontractno="106" groupcontractversion="40" cobno="1" productno="112" cobproductversion="1">
        <productname>Vattenskoterförsäkring</productname>
      </product>
  </group>
  <group>
   ...
  </grpup>
</products>

Model class:

public class GroupContract
{
    public string groupcontractno { get; set; }        
    public IList<Products> products { get; set; }

}

public class Products
{
    public string productname { get; set; }
}

I want to make list with group then in every group list has a products list. And this is what I tried:

List<Products> productList = new List<Products>();
List<GroupContract> groupcontractList = groupedData.Descendants("group")
                                    .Select(d => new GroupContract
                                    {
                                        groupcontractname = d.Attribute("groupcontractno").Value,
                                        products = productList.Select(e => new Products
                                        {
                                            productname = d.Descendants("product").Descendants("productname").First().Value
                                        }).ToList()
                                    }).ToList();

It did create group lists but in products lists are empty, anyone can correct my code would be much appreciated!

4
  • 1
    at products = productList.Select, productList is empty, it has nothing to select. Commented Jan 15, 2015 at 16:04
  • How is this groupedData variable initialized? Commented Jan 15, 2015 at 16:05
  • @PankajKapare groupedData is the xml object Commented Jan 15, 2015 at 16:07
  • How about changing this line productname = d.Descendants("product").Descendants("productname").First().Value to productname = d.Descendants("product").Descendants("productname").First().InnerText? Commented Jan 15, 2015 at 16:13

1 Answer 1

2

This is the query you're after:

List<GroupContract> groupcontractList = groupedData.Descendants("group")
                                .Select(d => new GroupContract
                                {
                                    groupcontractno = d.Attribute("groupcontractno").Value,
                                    products = d.Elements("product").Select(e => new Products
                                    {
                                        productname = d.Descendants("product").Descendants("productname").First().Value
                                    }).ToList()
                                }).ToList();

mainly, change:

products = productList.Select(...

to

products = d.Elements("product").Select(...

instead of productList.Select, use your group d and get all the product child elements into a list. Then you can toss your productList variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.