【问题标题】:creating an array from xml data in c#在 C# 中从 xml 数据创建一个数组
【发布时间】:2012-09-18 17:26:57
【问题描述】:

我在进行 API 调用时返回了以下 XML。我需要为每个<order_status><payment_status><fulfillment_status> 创建一个包含<status><description> 值的数组。谁能分享一些对我有帮助的知识?

<orderwave>
  <call_result>
    <order>
      <order_number>17377</order_number>
      <orderwave_order_number>RWD-336475921</orderwave_order_number>
      <order_status>
        <status>active</status>
        <description>This order is free to be charged and shipped.  It is open in the orderwave workflow.</description>
      </order_status>
      <payment_status>
        <status>declined</status>
        <description>This order has been declined by the payment network.</description>
      </payment_status>
      <fulfillment_status>
        <status>not shipped</status>
        <description>This order has not been allocated for shipment.</description>
      </fulfillment_status>
    </order>
  </call_result>
  <warning_count>0</warning_count>
  <warnings/>
  <error_count>0</error_count>
  <errors/>
</orderwave>

【问题讨论】:

  • 存储在这个数组中的对象的类型是什么?您是想简单地将 XML 分解为更小的 XDocument,还是想要一种更强类型的方法?
  • 你应该使用 linqToXML
  • 看看使用List&lt;T&gt;T 是一个对象来保存你想要的数据,然后使用 LINQ to XML 将文档解析到列表中。如果以后有时间,我会发布一个示例。

标签: c# xml arrays parsing


【解决方案1】:

LinqToXML 是你想要的,我相信。

我有一段时间没有这样做了,所以我的语法可能并不完美。

类似这样的:

var xmlSource = contacts.Load(@"../../return.xml");

var q = xmlSource.Descendants("order").SelectMany(x => x.Elements("order_status")

【讨论】:

    【解决方案2】:

    为了扩展我之前的评论,一个简单快捷的方法是使用List&lt;T&gt; ClassLINQ to XML

    使用一个类来保存每个订单的数据 - 例如:

    public class Order
    {
    
        public int OrderNumber { get; set; }
        public string OrderStatus { get; set; }
        public string OrderDescription { get; set; }
        public string PaymentStatus { get; set; }
        public string PaymentDescription { get; set; }
        public string FulfillmentStatus { get; set; }
        public string FulfillmentDescription { get; set; }
    }
    

    接下来,您可以将 XML 加载到 XDocument 中,使用 LINQ to XML 对其进行解析并创建 Order 对象列表:

    // Parse the XML string; you can also load the XML from a file.
    XDocument xDoc = XDocument.Parse("<orderwave>....</orderwave>");
    
    // Get a collection of elements under each order node
    var orders = (from x in xDoc.Descendants("order")
    // Use the data for each order node to create a new instance of the order class
                  select new Order
                  {
                      OrderNumber = ConvertTo.Int32(x.Element("order_number").Value),
                      OrderStatus = x.Element("order_status").Element("status").Value,
                      OrderDescription = x.Element("order_status").Element("description").Value,
                      PaymentStatus = x.Element("payment_status").Element("status").Value,
                      PaymentDescription = x.Element("payment_status").Element("description").Value,
                      FulfillmentStatus = x.Element("fulfillment_status").Element("status").Value,
                      FulfillmentDescription = x.Element("fulfillment_status").Element("description").Value
                  }).ToList();
              // Convert the result to a list of Order objects
    

    这是我没有测试的想法,但如果您想使用列表而不是数组,它应该为您指明正确的方向。

    【讨论】:

      猜你喜欢
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多