在父节点中恢复同级值

本文关键字:恢复 父节点 | 更新日期: 2023-09-27 18:26:23

<po-response xmlns="http://test.com/oms/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="rest/oms/export/v3/purchase-order.xsd">
  <!--Generated on host [spapp403p.prod.ch4.s.com]-->
  <purchase-order>
    <customer-order-confirmation-number>335342876</customer-order-confirmation-number>
    <customer-email>123456@test.com</customer-email>
    <po-number>12345</po-number>
    <po-date>2012-02-29</po-date>
    <po-time>13:02:59</po-time>
    <po-number-with-date>201202292988262</po-number-with-date>
    <unit>9301</unit>
    <site>Test</site>
    <channel>VD</channel>
    <location-id>1234</location-id>
    <expected-ship-date>2013-06-05</expected-ship-date>
    <shipping-detail>
      <ship-to-name>JOHN DOH</ship-to-name>
      <address>1234 MADE UP LANE</address>
      <city>BLAH</city>
      <state>KK</state>
      <zipcode>1234</zipcode>
      <phone>666123456</phone>
      <shipping-method>Ground</shipping-method>
    </shipping-detail>
    <customer-name>JOHN DOH</customer-name>
    <po-line>
      <po-line-header>
        <line-number>3</line-number>
        <item-id>ABC-12345</item-id>
        <item-name>Professional Chrome Dumbbell 1</item-name>
        <selling-price-each>12.76</selling-price-each>
        <commission>1.52</commission>
        <order-quantity>2</order-quantity>
        <shipping-and-handling>12.66</shipping-and-handling>
      </po-line-header>
      <po-line-detail>
        <po-line-status>NEW</po-line-status>
        <quantity>2</quantity>
      </po-line-detail>
    </po-line>
    <po-line>
      <po-line-header>
        <line-number>2</line-number>
        <item-id>BCD-33022</item-id>
        <item-name>Professional Chrome Dumbbell 2</item-name>
        <selling-price-each>13.82</selling-price-each>
        <commission>1.14</commission>
        <order-quantity>2</order-quantity>
        <shipping-and-handling>18.66</shipping-and-handling>
      </po-line-header>
      <po-line-detail>
        <po-line-status>NEW</po-line-status>
        <quantity>2</quantity>
      </po-line-detail>
    </po-line>
    <po-line>
      <po-line-header>
        <line-number>1</line-number>
        <item-id>CDE-33021</item-id>
        <item-name>Professional Chrome Dumbbell 3</item-name>
        <selling-price-each>15.88</selling-price-each>
        <commission>1.76</commission>
        <order-quantity>2</order-quantity>
        <shipping-and-handling>18.68</shipping-and-handling>
      </po-line-header>
      <po-line-detail>
        <po-line-status>NEW</po-line-status>
        <quantity>1</quantity>
      </po-line-detail>
    </po-line>
    <order-total-sell-price>42.92</order-total-sell-price>
    <total-commission>1.42</total-commission>
    <total-shipping-handling>46.00</total-shipping-handling>
    <balance-due>67.50</balance-due>
    <sales-tax>15.13</sales-tax>
    <po-status>New</po-status>
  </purchase-order>
<purchase-order>
.
.
.
我需要

一种方法来恢复所有细节,特别是我遇到了一个问题,因为在上面的情况下可能有任意数量的 's,其中有三个,我需要在 中循环浏览这些,然后转到下一个。

我使用 XDcoument 使用以下代码,它只在一个 po 行上工作正常,但无法让它与多个 po 行一起使用。

xmlDoc = XDocument.Parse(sr.ReadToEnd());
XNamespace ns = "http://test.com/oms/v3";
var purchaseOrders = from purchaseOrder in xmlDoc.Descendants(ns + "purchase-order")
    select new
    {        
         PurcaseOrderNo = purchaseOrder.Element(ns + "po-number").Value,
         PurchaseDate = purchaseOrder.Element(ns + "po-date").Value,
         CustomerFullName = purchaseOrder.Element(ns + "customer-name").Value,
         ItemId = purchaseOrder.Element(ns + "po-line").Element(ns + "po-line-header").Element(ns + "item-id").Value,
    };

我已经考虑过使用 foreach 循环并使用类似的东西遍历节点,但我需要带回该顺序的其他信息,而不仅仅是将结果限制为节点中的内容

foreach (XElement po in xmlDoc.Descendants(ns + "po-line"))
{
    string ItemId = po.Element(ns + "po-line-header").Element(ns + "item-id").Value;
    string SellingPrice = po.Element(ns + "po-line-header").Element(ns + "selling-price-each").Value;                                                        
}

我正在寻找最好的方法,也许需要两者的结合或一种新的方法?

所以我需要的结果是一行,例如,对于每个采购订单,客户详细信息将相同,但 itemId 将更改:

 customer email, po-number, ship-to name, item-id
    123456@test.com, 12345, JOHN DOH, ABC-12345
    123456@test.com, 12345, JOHN DOH, BCD-33022

在父节点中恢复同级值

目前还不清楚你想要什么,但这可能是你所需要的:

 // Earlier query as before...
 select new
 {
     PurcaseOrderNo = purchaseOrder.Element(ns + "po-number").Value,
     PurchaseDate = purchaseOrder.Element(ns + "po-date").Value,
     CustomerFullName = purchaseOrder.Element(ns + "customer-name").Value,
     ItemIds = purchaseOrder.Elements(ns + "po-line")
                            .Elements(ns + "po-line-header")
                            .Elements(ns + "item-id")
                            .Select(x => x.Value)
                            .ToList()
 };

这将使您在每个结果中List<string> ItemIds

编辑:如果你想要每个po-line一个结果,你可以使用:

var query = from order in xmlDoc.Descendants(ns + "purchase-order")
            from line in order.Elements(ns + "po-line")
            select new
            {        
                PurcaseOrderNo = order.Element(ns + "po-number").Value,
                PurchaseDate = order.Element(ns + "po-date").Value,
                CustomerFullName = order.Element(ns + "customer-name").Value,
                ItemId = line.Element(ns + "po-line-header")
                             .Element(ns + "item-id")
                             .Value
            };

我建议你为每个对象创建类...即:

public class PurchaseOrder
{
    XElement self;
    public PurchaseOrder(XElement self) { this.self = self; }
    public int Number
    {
         get { return self.Get<int>("po-number", 0); }
    }
    public POLine[] Lines
    {
         get { return _Lines ?? (_Lines = self.GetEnumerable("po-line", xe => new POLine(xe)).ToArray()); }
    }
    POLine[] _Lines;
}
public class POLine
{
    XElement self;
    public POLine(XElement self) { this.self = self; }
    public string ItemName
    {
        get { self.Get("po-line-header/item-name", string.Empty); }
    }
}

然后,您可以像这样获取所有采购订单:

XElement xfile = // load your file
PurchaseOrder[] orders = xfile.GetEnumerable("purchase-order", xe => new PurchaseOrder(xe)).ToArray();
foreach(PurchaseOrder order in orders)
{
    // do something with the order number
    int number = order.Number;
    ...
    foreach(POLine line in order.Lines)
    {
        // do something with the item name
        string itemName = line.ItemName;
        ...
    }
    ...
}

您可以使用我在这里使用的扩展方法:http://searisen.com/xmllib/extensions.wiki