在父节点中返回兄弟值

时间:2012-03-02 17:44:22

标签: c# xml linq-to-xml

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

我需要一种方法来恢复所有细节,特别是我有问题,因为在上面的情况下可以有任意数量的有三个,我需要在这个内部循环这些然后转到下一个。

我正在使用以下代码使用XDcoument,它只使用一个po-line工作正常,但是不能让它与多个po-line一起工作。

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

我建议你为每个对象制作课程...... aka:

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