【问题标题】:Parsing xml using XDocument - Deserialization使用 XDocument 解析 xml - 反序列化
【发布时间】:2013-09-12 23:45:56
【问题描述】:

我有以下xml

<?xml version="1.0" encoding="UTF-8"?>
<Actions>
    <add order-id="1" symbol="ABC" price="5" quantity="200" />
    <add order-id="2" symbol="123" price="15" quantity="100" />
    <add order-id="3" symbol="ABC" price="5" quantity="300" />
    <add order-id="4" symbol="ABC" price="7" quantity="150" />
    <edit order-id="1" price="7" quantity="200" />
    <remove order-id="4" />
    <add order-id="5" symbol="123" price="17" quantity="300" />
    <add order-id="6" symbol="123" price="12" quantity="150" />
    <edit order-id="3" price="7" quantity="200" />
    <remove order-id="5" />
</Actions>

我需要使用 linq 将其解析为以下对象结构:

 internal class OrderAction
    {
        private readonly Action action;
        private readonly Order order;
        public Action Action { get { return action; }}
        public Order Order { get { return order; }}

        public OrderAction(Action action,Order order)
        {
            this.action = action;
            this.order = order;
        }
    }

其中一个动作是public enum Action { ADD, REMOVE, EDIT }

顺序如下:

class Order
    {
        public Order(long orderId, String symbol, int price, int quantity)
        {
            this.orderId = orderId;
            this.symbol = symbol;
            this.price = price;
            this.quantity = quantity;
        }

        public long orderId { get; private set; }
        public String symbol { get; private set; }
        public int price { get; private set; }
        public int quantity { get; private set; }
}

我需要将 xml 加载到 xdocument 中并在文件中找到所有 IEnumerable 的 orderactions。我怎样才能做到这一点,而不诉诸于序列化属性?

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    在“编辑和删除”的情况下,缺少的Order 属性的值应该是多少?我假设stringint 的默认值...

    这将是 LINQ 语句(假设您的 XDocument 被称为 doc):

    var result = doc
        .Root
        .Elements()
        .Select(elem => new OrderAction
                (
                    (Action)Enum.Parse(typeof(Action), elem.Name.LocalName.ToUpper()),
                    new Order
                    (
                        elem.Attributes("order-id").Select(x => long.Parse(x.Value)).FirstOrDefault(),
                        elem.Attributes("symbol").Select(x => x.Value).FirstOrDefault(),
                        elem.Attributes("price").Select(x => int.Parse(x.Value)).FirstOrDefault(),
                        elem.Attributes("quantity").Select(x => int.Parse(x.Value)).FirstOrDefault()
                    )
                ));
    

    【讨论】:

    • 什么是根?这是编译吗?
    • RootXDocument的一个属性(返回rootXElement)...是的,编译运行成功
    • 我明白了...您可以通过使用 XElement 打开文档来缩短它
    【解决方案2】:

    这是我最好的镜头

         XElement doc = XElement.Load("d:\\tst.xml");
    
         List<OrderAction> lst = doc.Elements().Select(x =>
           new OrderAction(
              (Action)
              Enum.Parse(typeof (Action),
                         x.Name.ToString().ToUpper()),
              new Order(
                 long.Parse(x.Attribute("order-id").Value),
                 x.Attribute("symbol") != null ? x.Attribute("symbol").Value : "",
                 x.Attribute("price") != null ? int.Parse(x.Attribute("price").Value) : 0,
                 x.Attribute("quantity") != null ? int.Parse(x.Attribute("quantity").Value): 0))
            ).ToList();
    

    花了我一段时间...我不习惯这么大的 linq 解析

    【讨论】:

    • 不应该因为Attribute返回null而导致编辑/删除标签崩溃???您还要求提供错误的属性...
    • @olydis 让我解决这个问题
    • @olydis tnx,没注意到
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多