简体   繁体   English

在C#中从xml数据创建数组

[英]creating an array from xml data in c#

I have the following XML returned when I make an API call. 进行API调用时,返回了以下XML。 I need to create an array that holds the <status> and <description> values for each of <order_status> , <payment_status> , and <fulfillment_status> . 我需要创建一个数组,为每个<order_status><payment_status><fulfillment_status>分别保存<status><description>值。 Can anyone share some knowledge that would help me? 谁能分享一些对我有帮助的知识?

<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>

LinqToXML is what you want I believe. 我相信LinqToXML是您想要的。

I haven't done this in a while so my syntax might not be perfect. 我已经有一段时间没有这样做了,所以我的语法可能并不完美。

Something like this: 像这样:

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

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

To expand upon my earlier comment, an easy and quick way to do this is with List<T> Class and LINQ to XML . 为了扩展我之前的评论,使用List<T> ClassLINQ to XML可以轻松而快速地完成此操作。

Use a class to hold the data for each order - for example: 使用一个类来保存每个订单的数据-例如:

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; }
}

Next, you can load the XML into an XDocument , parse it with LINQ to XML and create a List of Order objects: 接下来,您可以将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

This is off the top of my head without testing it, but it should point you in the right direction if you want to use Lists instead of arrays. 如果不进行测试,这是我的首要任务,但是如果您要使用列表而不是数组,它应该为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM