Search code examples
c#asp.netxmllinqxelement

Dynamic Nested XElements


I wish to create an XML and this will include something as follows :-

<?xml version="1.0" encoding="utf-8"?>
<Groups>
    <Group>
        <Id>1</Id>
        <GroupName>Group1</CategoryId>
        <Products>
            <Product>
                <ProductId>1</ProductId>
                <ProductName>Apples</ProductName>
            </Product>
            <Product>
                <ProductId>2</ProductId>
                <ProductName>Oranges</ProductName>
            </Product>
            <Product>
                <ProductId>3</ProductId>
                <ProductName>Lemons</ProductName>
            </Product>
        </Products>
        <DateCreated></DateCreated>
        <DateModified></DateModified>
    </Group>
    <Group>
        <Id>2</Id>
        <GroupName>Group2</CategoryId>
        <Products>
            <Product>
                <ProductId>3</ProductId>
                <ProductName>Grapes</ProductName>
            </Product>
            <Product>
                <ProductId>4</ProductId>
                <ProductName>PineApple</ProductName>
            </Product>
        </Products>
        <DateCreated></DateCreated>
        <DateModified></DateModified>
    </Group>
</Groups>

As you can see from my example, amount of Product can vary from 1 group to the other.

How can I create a dynamic XML and also be able to read the same XML later.

At the moment my code to create the XML is as follows :

internal XElement ConstructGroupXML(int numberOfItems)
{
    XElement xmlList = new XElement("Groups",
        from a in dataModel.CreateGroupList(numberOfItems)
        select new XElement("Group",
            new XElement("Id", a.Id),
            new XElement("GroupName", a.GroupName),
            new XElement("Products",
                new XElement("ProductId", a.Products[i].Id),
                new XElement("ProductName", a.Products[i].ProductName),
                new XElement("CategoryId", a.Products[i].Category.Id),
                new XElement("CategoryName", a.Products[i].Category.CategoryName),
                new XElement("SubCategoryId", a.Products[i].SubCategory.Id),
                new XElement("SubCategoryName", a.Products[i].SubCategory.SubCategoryName),
            new XElement("DateCreated", a.DateCreated),
            new XElement("DateModified", a.DateModified)
        )
    );

    return xmlList;
}

The CreateGroupList method is returning an object with Groups and lists of Products embedded in these Groups, so for each Group I wish to loop inside the Product List and generate the XML.


Solution

  • Ok I managed to find the solution:

    from o in a.Products
    select new XElement("Products",
        new XAttribute("ProductId", o.Id),
        new XElement("ProductName", o.ProductName),
        new XElement("CategoryId", o.Category.Id),
        new XElement("CategoryName", o.Category.CategoryName),
        new XElement("SubCategoryId", o.SubCategory.Id),
        new XElement("SubCategoryName", o.SubCategory.SubCategoryName),
    

    Now I just need to find out how to read this XML