使用 ASP.Net 的 Linq 到 xml

发布于 2024-12-04 20:59:00 字数 1592 浏览 0 评论 0原文

我正在尝试将订单的所有元素填充到列表框中。列表框仅添加集合,而不是元素。请帮忙!

XDocument doc = XDocument.Load(Server.MapPath("App_Data/Orders.xml"));

        string order = ddl.SelectedValue;

        var results = doc.Descendants("Order").Where(o => o.Attribute("OrderNumber").Value == "SO43659")
                         .Select(o => o.Elements("LineItem"));

        foreach (var r in results)
        {
          ListBox1.Items.Add(new ListItem(r.ToString()));

        }

xml片段:

<Order OrderNumber="SO43659">
    <LineItem Line="1" PID="349" Qty="1" Price="2024.9940" Freight="50.6249" />
    <LineItem Line="2" PID="350" Qty="3" Price="2024.9940" Freight="151.8746" />
    <LineItem Line="3" PID="351" Qty="1" Price="2024.9940" Freight="50.6249" />
    <LineItem Line="4" PID="344" Qty="1" Price="2039.9940" Freight="50.9999" />
    <LineItem Line="5" PID="345" Qty="1" Price="2039.9940" Freight="50.9999" />
    <LineItem Line="6" PID="346" Qty="2" Price="2039.9940" Freight="101.9997" />
    <LineItem Line="7" PID="347" Qty="1" Price="2039.9940" Freight="50.9999" />
    <LineItem Line="8" PID="229" Qty="3" Price="28.8404" Freight="2.1630" />
    <LineItem Line="9" PID="235" Qty="1" Price="28.8404" Freight="0.7210" />
    <LineItem Line="10" PID="218" Qty="6" Price="5.7000" Freight="0.8550" />
    <LineItem Line="11" PID="223" Qty="2" Price="5.1865" Freight="0.2593" />
    <LineItem Line="12" PID="220" Qty="4" Price="20.1865" Freight="2.0187" />
  </Order>

I am trying to poulate all the elements of an order to a listbox. The listbox just adds the collection, not the elements. Please help!

XDocument doc = XDocument.Load(Server.MapPath("App_Data/Orders.xml"));

        string order = ddl.SelectedValue;

        var results = doc.Descendants("Order").Where(o => o.Attribute("OrderNumber").Value == "SO43659")
                         .Select(o => o.Elements("LineItem"));

        foreach (var r in results)
        {
          ListBox1.Items.Add(new ListItem(r.ToString()));

        }

xml fragment:

<Order OrderNumber="SO43659">
    <LineItem Line="1" PID="349" Qty="1" Price="2024.9940" Freight="50.6249" />
    <LineItem Line="2" PID="350" Qty="3" Price="2024.9940" Freight="151.8746" />
    <LineItem Line="3" PID="351" Qty="1" Price="2024.9940" Freight="50.6249" />
    <LineItem Line="4" PID="344" Qty="1" Price="2039.9940" Freight="50.9999" />
    <LineItem Line="5" PID="345" Qty="1" Price="2039.9940" Freight="50.9999" />
    <LineItem Line="6" PID="346" Qty="2" Price="2039.9940" Freight="101.9997" />
    <LineItem Line="7" PID="347" Qty="1" Price="2039.9940" Freight="50.9999" />
    <LineItem Line="8" PID="229" Qty="3" Price="28.8404" Freight="2.1630" />
    <LineItem Line="9" PID="235" Qty="1" Price="28.8404" Freight="0.7210" />
    <LineItem Line="10" PID="218" Qty="6" Price="5.7000" Freight="0.8550" />
    <LineItem Line="11" PID="223" Qty="2" Price="5.1865" Freight="0.2593" />
    <LineItem Line="12" PID="220" Qty="4" Price="20.1865" Freight="2.0187" />
  </Order>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

耀眼的星火 2024-12-11 20:59:00
var results = doc.Descendants("Order")
                 .Where(o => o.Attribute("OrderNumber").Value == "SO43659")
                 .FirstOrDefault();

foreach (var r in results.Elements("LineItem"))
{
    ListBox1.Items.Add(new ListItem(r.ToString()));
}
var results = doc.Descendants("Order")
                 .Where(o => o.Attribute("OrderNumber").Value == "SO43659")
                 .FirstOrDefault();

foreach (var r in results.Elements("LineItem"))
{
    ListBox1.Items.Add(new ListItem(r.ToString()));
}
韵柒 2024-12-11 20:59:00

如果您没有使用 var,您就会看到问题所在 - results 的每个元素都是一个元素序列。这是没有 var 的代码:

IEnumerable<IEnumerable<XElement>> results =
   doc.Descendants("Order")
      .Where(o => o.Attribute("OrderNumber").Value == "SO43659")
      .Select(o => o.Elements("LineItem"));

foreach (IEnumerable<XElement> r in results)
{
    ListBox1.Items.Add(new ListItem(r.ToString()));

}

我怀疑这不是您想要的。你可以用这个代替:

// Will go bang if there isn't exactly one matching order
IEnumerable<XElement> results =
   doc.Descendants("Order")
      .Where(o => o.Attribute("OrderNumber").Value == "SO43659")
      .Single()
      .Select(o => o.Elements("LineItem"));

或者:

// Will find *all* the LineItem elements under *all* matching orders
IEnumerable<XElement> results =
   doc.Descendants("Order")
      .Where(o => o.Attribute("OrderNumber").Value == "SO43659")
      .SelectMany(o => o.Elements("LineItem"));

If you didn't use var, you'd see what's wrong - each elements of results is a sequence of elements. Here's your code without var:

IEnumerable<IEnumerable<XElement>> results =
   doc.Descendants("Order")
      .Where(o => o.Attribute("OrderNumber").Value == "SO43659")
      .Select(o => o.Elements("LineItem"));

foreach (IEnumerable<XElement> r in results)
{
    ListBox1.Items.Add(new ListItem(r.ToString()));

}

I suspect that's not what you're after. You could use this instead:

// Will go bang if there isn't exactly one matching order
IEnumerable<XElement> results =
   doc.Descendants("Order")
      .Where(o => o.Attribute("OrderNumber").Value == "SO43659")
      .Single()
      .Select(o => o.Elements("LineItem"));

Or:

// Will find *all* the LineItem elements under *all* matching orders
IEnumerable<XElement> results =
   doc.Descendants("Order")
      .Where(o => o.Attribute("OrderNumber").Value == "SO43659")
      .SelectMany(o => o.Elements("LineItem"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文