【问题标题】:Linq to XML holding relationshipLinq to XML 持有关系
【发布时间】:2013-01-16 11:17:48
【问题描述】:

我有这种结构的 XML 文档:

<dipOrders>
    <interchangeInfo senderEdiCode="LSC58" senderEdiCodeQal="ZZ" receiverEdiCode="15274" receiverEdiCodeQal="ZZ" syntax="X12" syntaxId="X" syntaxVersion="003010"/>
    <order orderNumber="219299" orderDate="2012-12-05T00:00:00" validityDate="2012-12-05T00:00:00">
        <buyer name="LEAR MTO">
            <partyCode buyerCode="811567924"/>
        </buyer>
        <supplier name="BRIDGE OF WEIR LEATHER CO">
            <partyCode buyerCode="749630"/>
        </supplier>
        <orderConsignee name="LEAR MEXICAN SEATING CORP">
            <partyCode buyerCode="LSC59"/>
            <orderLine description="LEA DC 378 HERO 6RSB 5B8" orderNumber="246767" engineeringChangeNumber="N">
                <partyCode buyerCode="DC378105H6RSB5B8AA"/>
                <cumulativeQuantity date="2012-12-04T00:00:00" quantity="0"/>
                <orderQuantity quantity="0" commitmentLevel="Firm" timingQualifier="Weekly" shipDate="2012-12-05T00:00:00"/>
            </orderLine>
            <orderLine description="LEA DC 378 HERO 6RSB 5V0" orderNumber="246767" engineeringChangeNumber="N">
                <partyCode buyerCode="DC378105H6RSB5V0AA"/>
                <cumulativeQuantity date="2012-12-04T00:00:00" quantity="0"/>
                <orderQuantity quantity="0" commitmentLevel="Firm" timingQualifier="Weekly" shipDate="2012-12-05T00:00:00"/>
                <orderQuantity quantity="600" commitmentLevel="Firm" timingQualifier="Weekly" shipDate="2012-12-06T00:00:00"/>
            </orderLine>

这是我访问 orderLine 项目属性的方式:

List<int> orderNumbers = doc.Descendants("orderLine").Select(x => int)x.Attribute("orderNumber")).ToList();
List<string> descriptions = doc.Descendants("orderLine").Select(x => (string)x.Attribute("description")).ToList();
List<string> buyerCodes = doc.Descendants("orderLine").Select(x => (string)x.Element("partyCode").Attribute("buyerCode")).ToList(); 

但是,orderLine 可以有不同数量的 orderQuantity 节点。如何在 Linq 语句中解决它以获得 orderLine 和 orderQuantity 之间的适当关系?

【问题讨论】:

    标签: c# .net xml linq linq-to-xml


    【解决方案1】:

    你可以使用这个获取 orderQuantity 元素

    var data = doc.Descendants("orderLine").
                   Descendants("orderQuantity")
                 .Select(x =>   new {paernt = x.Parent, x}).ToList();
    

    也这样做

    var data = doc.Descendants("orderLine").
                   Descendants("orderQuantity")
                 .Select(x =>   new {OrderNumber= (int) x.Parent.Attribute("orderNumber"), 
                                                                          x}).ToList();
    

    【讨论】:

    • 但是这样我得到所有“数量”而不知道它们属于哪个 orderLine
    • @Bartosz - 以上更新将为您提供子元素和父元素,即 orderline 和 order qunatity...
    • 但这给了我一般数据对象,我现在如何挖掘它以获得不同属性的值
    • @Bartosz - 为此我认为你需要循环或选择新的将像这个新的 {paerntOrdernumber = x.Parent.Attribute("orderNumber"), x}
    【解决方案2】:

    尝试使用SelectMany,就像在下一个示例中一样

    var r = doc.Descendants("orderLine")
               .SelectMany(node => node.Elements("orderQuantity")
                                       .Select(el => new {OrderNumber = (int)node.Attribute("orderNumber"), Quantity = (int)el.Attribute("quantity")}));
    

    现在您有一系列匿名对象,指示订单号和相应数量,例如:

    246767 0 
    246767 600 
    

    【讨论】:

    • 但是我怎样才能保持关系,知道哪个数量属于哪个orderLine
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多