如何在 Xml Linq 上创建子项

发布于 2024-09-13 15:10:08 字数 3607 浏览 5 评论 0原文

在northwind数据库中。我使用order和orderDetails表。我想创建一个xml文件,其中order显示在XML和orderdetails上的项目标签上显示在子商品上。假设orderid=1显示在Item标签上,则OrderID=1orderdetais表格信息 显示在子项目上。

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-09-20 15:10:42
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)
        )
    ));
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)
        )
    ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文