-1

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.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
JMon
  • 3,387
  • 16
  • 63
  • 102
  • 1
    Can you add the code for `CreateGroupList(numberOfItems)`? You're missing a closing parentheses on the line `new XElement("DateModified", a.DateModified)));` it should be `new XElement("DateModified", a.DateModified))));` – Ryan Gates Mar 13 '13 at 16:14
  • yes you are right about the parenthesis. The AddGroup is nothing special, just looping on some products and adding them to a list, and attaching them to the group. Its a List inside a Group object – JMon Mar 13 '13 at 16:20

1 Answers1

1

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

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
JMon
  • 3,387
  • 16
  • 63
  • 102