views:

31

answers:

1

In northwind database .I use order and orderDetails table.I want to create a xml file ,where order show on Item tag on XML and orderdetails show on sub item.Suppose orderid=1 show on Item tag then orderdetais**table information of **OrderID=1 show on sub-Item.

 XElement xml = new XElement("MyMenu",
                      from c in db.Orders
                      //where (c.ParentID == 0)
                      orderby c.OrderID
                      select new XElement("Item",
                                c.OrderID == null ? null : new XAttribute("OrderID", c.OrderID),
                                 c.ShipName == null ? null : new XAttribute("ShipName", c.ShipName),
                                  c.OrderDate == null ? null : new XAttribute("OrderDate", c.OrderDate)


                                )
                      );

            xml.Save(@"C:\contacts.xml");

What should i need to add on my above syntax.Show me syntax.Thanks in advance.I work on C#.Above syntax show the bellow output:

<?xml version="1.0" encoding="utf-8"?>
<MyMenu>
  <Item OrderID="10248" ShipName="Vins et alcools Chevalier" OrderDate="1996-07-04T00:00:00" />
  <Item OrderID="10249" ShipName="Toms Spezialitäten" OrderDate="1996-07-05T00:00:00" />
  <Item OrderID="10250" ShipName="Hanari Carnes" OrderDate="1996-07-08T00:00:00" />
</MyMenu>

I want bellow out put

    <MyMenu>
      <Item OrderID="10248" ShipName="Vins et alcools Chevalier" OrderDate="1996-07-04T00:00:00" >
      <SubItem>  
       ..........
       ..........Contain detailTable Information for OrderID="10248"
       ...........
    </SubItem>
</Item>      
    </MyMenu>

My bellow syntax fail to create above output:

 XElement xml = new XElement("MyMenu",
                      //from c in db.Orders
                       from c in db.Orders
                       join od in db.Order_Details on c.OrderID equals od.OrderID
                      //where (c.ParentID == 0)
                      orderby c.OrderID
                      select new XElement("Item",
                                c.OrderID == null ? null : new XAttribute("OrderID", c.OrderID),
                                 c.ShipName == null ? null : new XAttribute("ShipName", c.ShipName),
                                  c.OrderDate == null ? null : new XAttribute("OrderDate", c.OrderDate),
                          new XElement("SubItem",
                               od.OrderID == null ? null : new XAttribute("OrderID", od.OrderID),
                               od.ProductID == null ? null : new XAttribute("ProductID", od.ProductID),
                               od.Quantity == null ? null : new XAttribute("Quantity", od.Quantity)
                               )


                                )
                      );

Above syntax create bellow output:

<?xml version="1.0" encoding="utf-8"?>
<MyMenu>
  <Item OrderID="10250" ShipName="Hanari Carnes" OrderDate="1996-07-08T00:00:00">
    <SubItem OrderID="10250" ProductID="41" Quantity="10" />
  </Item>
  <Item OrderID="10250" ShipName="Hanari Carnes" OrderDate="1996-07-08T00:00:00">
    <SubItem OrderID="10250" ProductID="51" Quantity="35" />
  </Item>
  <Item OrderID="10250" ShipName="Hanari Carnes" OrderDate="1996-07-08T00:00:00">
    <SubItem OrderID="10250" ProductID="65" Quantity="15" />
  </Item>
</MyMenu>

How to put OrderID="10250" all subItem under one Item tag?