如何在 Xml Linq 上创建子项
在northwind数据库中。我使用order和orderDetails表。我想创建一个xml文件,其中order显示在XML和orderdetails上的项目标签上显示在子商品上。假设orderid=1显示在Item标签上,则OrderID=1的orderdetais表格信息 显示在子项目上。
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" 所有子项目放在一个项目标签下?
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 orderdetaistable 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LINQ to XML:通过 LINQ 创建复杂的 XML
LINQ to XML : Creating complex XML through LINQ