在northwind数据库中。我使用 order和orderDetails 表。我想创建一个xml文件,其中 order 显示在XML上的Item标记和 orderdetails < / strong>在子商品上显示。在项目代码上显示orderid = 1,然后 orderdetais 订单ID = 1 的表格信息 显示在子项目上。
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");
我需要在上面的语法中添加什么。请说明语法。提前谢谢。我在C#上工作。以上语法显示波纹管输出:
<?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>
我想要提出
<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>
我的波纹管语法无法创建上述输出:
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)
)
)
);
以上语法创建波纹管输出:
<?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>
如何将OrderID =“10250”所有subItem放在一个Item标记下?
答案 0 :(得分:1)
答案 1 :(得分:0)
XElement xml = new XElement("MyMenu",
//from c in db.Orders
from c in db.Orders
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),
from od in db.Order_Details
where od.OrderID == c.OrderID
select 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)
)
));