Linq to XML - 反序列化为对象

发布于 2024-12-19 14:08:54 字数 656 浏览 1 评论 0原文

我已经能够让 Linq to XML 工作,但我想看看是否有更有效的方法将元素应用到对象,而不是在遍历 xml 后放置 foreach 语句?我知道你可以使用兰巴表达式,但不确定如何将其应用于此或是否可能?

任何建议都非常感激。

List<Order> myOrders = new List<Order>();

var orders = from order in xdoc.Descendants("Order")
             select new{
                  OrderNumber = order.Element("OrderNumber").Value,
                  OrderDate = order.Element("OrderDate").Value,
                  OrderTotal = order.Element("OrderTotal").Value
             };

foreach(var ord in orders)
{
   myOrders.OrderNumber = ord.OrderNumber;
   myOrders.OrderDate = ord.OrderDate;
   myOrders.OrderTotal = ord.OrderTotal;
}

I have been able to get Linq to XML to work, but I wanted to see if there was a more efficient way of applying the elements to an object rather then putting a foreach statement after I have traversed the xml? I know you can use lamba expressions, but not sure how to apply that to this or if that is possible?

Any suggestions greatly appreciated.

List<Order> myOrders = new List<Order>();

var orders = from order in xdoc.Descendants("Order")
             select new{
                  OrderNumber = order.Element("OrderNumber").Value,
                  OrderDate = order.Element("OrderDate").Value,
                  OrderTotal = order.Element("OrderTotal").Value
             };

foreach(var ord in orders)
{
   myOrders.OrderNumber = ord.OrderNumber;
   myOrders.OrderDate = ord.OrderDate;
   myOrders.OrderTotal = ord.OrderTotal;
}

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

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

发布评论

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

评论(3

小矜持 2024-12-26 14:08:54

只需选择 Order 实例:

var orders = from order in xdoc.Descendants("Order")
             select new Order {
                  OrderNumber = order.Element("OrderNumber").Value,
                  OrderDate = order.Element("OrderDate").Value,
                  OrderTotal = order.Element("OrderTotal").Value
             };

orders 的类型将为 IEnumerable,但如果您需要列表:

List<Order> myOrders = orders.ToList();

Simply select Order instances:

var orders = from order in xdoc.Descendants("Order")
             select new Order {
                  OrderNumber = order.Element("OrderNumber").Value,
                  OrderDate = order.Element("OrderDate").Value,
                  OrderTotal = order.Element("OrderTotal").Value
             };

orders will be of type IEnumerable<Order>, but if you need a list:

List<Order> myOrders = orders.ToList();
小忆控 2024-12-26 14:08:54

使用以下内容替换您发布的代码

List<Order> myOrders = (from order in xdoc.Descendants("Order")
                        select new Order {
                        OrderNumber = order.Element("OrderNumber").Value,
                        OrderDate = order.Element("OrderDate").Value,
                        OrderTotal = order.Element("OrderTotal").Value
                        }).ToList();

use the following as replacement for the code you posted

List<Order> myOrders = (from order in xdoc.Descendants("Order")
                        select new Order {
                        OrderNumber = order.Element("OrderNumber").Value,
                        OrderDate = order.Element("OrderDate").Value,
                        OrderTotal = order.Element("OrderTotal").Value
                        }).ToList();
长不大的小祸害 2024-12-26 14:08:54
List<Order> myOrders = new List<Order>();

var orders = xdoc.Descendants("Order").Select(x=> new Order{ 
    OrderNumber = x.Element("OrderNumber").Value,
    OrderDate = x.Element("OrderDate").Value,
    OrderTotal = x.Element("OrderTotal").Value
    });

myOrders.AddRange(orders) // or just orders.ToList();
List<Order> myOrders = new List<Order>();

var orders = xdoc.Descendants("Order").Select(x=> new Order{ 
    OrderNumber = x.Element("OrderNumber").Value,
    OrderDate = x.Element("OrderDate").Value,
    OrderTotal = x.Element("OrderTotal").Value
    });

myOrders.AddRange(orders) // or just orders.ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文