1

API 呼び出しを行うと、次の XML が返されます。、、およびのそれぞれの<status>および<description>の値を保持する配列を作成する必要があります。誰かが私に役立つ知識を共有できますか?<order_status><payment_status><fulfillment_status>

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

2 に答える 2

0

以前のコメントを拡張するために、これを行う簡単で迅速な方法はList<T> ClassLINQtoXMLを使用することです。

クラスを使用して、各注文のデータを保持します-例:

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

これはテストせずに頭から離れていますが、配列の代わりにリストを使用したい場合は、正しい方向を示しているはずです。

于 2012-09-19T03:18:24.797 に答える
0

LinqToXML は、私が信じているものです。

しばらくこれを行っていないため、構文が完全ではない可能性があります。

このようなもの:

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

var q = xmlSource.Descendants("order").SelectMany(x => x.Elements("order_status")
于 2012-09-18T19:16:06.697 に答える