Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I want to create a xml as given below.

XML
<Root>
  <CustomerDetail>
    <Customer>
      <Id>CUST0001</Id>
      <Name>Jackson</Name>
      <Address>12th Street</Address>
      </Customer>
    <Customer>
      <Id>CUST0002</Id>
      <Name>Johnson</Name>
      <Address>15th Main Road</Address>
    </Customer>
  </CustomerDetail>
  <OrderDetail>
    <Placed>
      <Order>
        <CustomerId>CUST0001</CustomerId>
        <OrderId>ORDER0012345</OrderId>
        <Total>1000</Total>
        <Item>
          <ItemId>ITEM00025</ItemId>
          <Price>255</Price>
        </Item>
        <Item>
          <ItemId>ITEM00026</ItemId>
          <Price>745</Price>
        </Item>
      </Order>
      <Order>
        <CustomerId>CUST0002</CustomerId>
        <OrderId>ORDER0012346</OrderId>
        <Total>450</Total>
        <Item>
          <ItemId>ITEM00095</ItemId>
          <Price>450</Price>
        </Item>
      </Order>
    </Placed>
    <Delivered>
      <Order>
        <OrderId>ORDER0012385</OrderId>
        <Total>800</Total>
      </Order>
      <Order>
        <OrderId>ORDER0012305</OrderId>
        <Total>550</Total>
      </Order>
    </Delivered>
    <Removed>
      <Order>
        <OrderId>ORDER0012300</OrderId>
      </Order>
      <Order>
        <OrderId>ORDER0012301</OrderId>
      </Order>
    </Removed>
  </OrderDetail>
</Root>


I planed to send as a class object like

C#
public class Report
{
    public Report()
    {
    }
    public int CustId;
    public string name;
    public string address;

    public int OrderId;
    public int total;

    public int ItemId;
    public int price;

    public string operation;//Placed, Removed, Delivered
}


Convert the above class objects into the xml of format mentioned in top.

Please help me to achieve it.

Thanks in advance..

Regards
Selvam S
Posted
Comments
Sergey Alexandrovich Kryukov 2-Nov-12 23:28pm    
Why LINQ, why lambda? Do you know about LINQ and lambda anything except their names?
--SA
sels1987 3-Nov-12 0:16am    
Do you have answer for the question..?
Sergey Alexandrovich Kryukov 3-Nov-12 18:14pm    
I don't think it's a valid answer. I do know what to do with XML, of course. The question is if you really want to keep this schema and why, or you want to do something really robust...
--SA

1 solution

C#
private void LoadXML()
        {

            List<Report> customerList = new List<Report>();
            customerList.Add(new Report() { CustId = 1, name = "Jackson", address = "12th Street" });
            customerList.Add(new Report() { CustId = 2, name = "Johnson", address = "15th Main Road" });

            List<Report> orderList = new List<Report>();
            List<Report> itemList = new List<Report>();
            orderList.Add(new Report() { CustId = 1, OrderId = 1, total = 1000, operation = "Placed" });
            orderList.Add(new Report() { CustId = 1, OrderId = 2, total = 2000, operation = "Placed" });
            orderList.Add(new Report() { CustId = 1, OrderId = 3, total = 3000, operation = "Delivered" });
            orderList.Add(new Report() { CustId = 2, OrderId = 4, total = 4000, operation = "Delivered" });
            orderList.Add(new Report() { CustId = 2, OrderId = 5, operation = "Removed" });
            orderList.Add(new Report() { CustId = 2, OrderId = 6, operation = "Removed" });

            itemList.Add(new Report() { OrderId = 1, ItemId = 1, price = 1001 });
            itemList.Add(new Report() { OrderId = 1, ItemId = 2, price = 1002 });
            itemList.Add(new Report() { OrderId = 2, ItemId = 3, price = 1003 });
            itemList.Add(new Report() { OrderId = 2, ItemId = 4, price = 1004 });
            itemList.Add(new Report() { OrderId = 3, price = 1005 });
            itemList.Add(new Report() { OrderId = 3, price = 1006 });
            itemList.Add(new Report() { OrderId = 4, price = 1007 });
            itemList.Add(new Report() { OrderId = 4, price = 1008 });
            itemList.Add(new Report() { OrderId = 5 });
            itemList.Add(new Report() { OrderId = 5 });
            itemList.Add(new Report() { OrderId = 6 });
            itemList.Add(new Report() { OrderId = 6 });

            XElement xmlstring = new XElement("Root",
                new XElement("CustomerDetail",
                    customerList.Select(x => new XElement("Customer",
                        new XElement("Id", "CUST000" + x.CustId),
                        new XElement("Name", x.name),
                        new XElement("Address", x.address)))),
                        new XElement("OrderDetail", orderList.Any(x => x.operation == "Placed") ? 
                            AddTag(orderList.Where(s => s.operation == "Placed").ToList(), itemList, "Placed") : null,
                            orderList.Any(x => x.operation == "Delivered") ? 
                            AddTag(orderList.Where(s => s.operation == "Delivered").ToList(), itemList, "Delivered") : null,
                            orderList.Any(x => x.operation == "Removed") ? 
                            AddTag(orderList.Where(s => s.operation == "Removed").ToList(), itemList, "Removed") : null));
            System.Console.WriteLine(xmlstring.ToString());
        }

        private XElement AddTag(List<Report> order, List<Report> item, string operation)
        {
            return new XElement(operation,
                order.Select(x =>
                    new XElement("Order",
                        operation == "Placed" ? 
                        new XElement("CustomerId", "CUST000" + x.CustId) : null,
                        new XElement("OrderId", "ORDER000" + x.OrderId),
                        operation != "Removed" ? 
                        new XElement("Total", x.total) : null,
                        operation == "Placed" ? 
                        item.Where(y => y.OrderId == x.OrderId).Select(z =>
                            new XElement("Item",
                                new XElement("ItemId", "ITEM000" + z.ItemId),
                                new XElement("Price", z.price))) : null)));
        }

[Edit]Code block added[/Edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900