如何从linq查询中获得正确的值?

问题描述 投票:0回答:1

XML

<wd:Report_Entry>
        <wd:Cost_Center wd:Descriptor=\"8808 ECO Global Forwarding Austria\">
            <wd:ID wd:type=\"WID\">b81295748006019ec87c29edcb020044</wd:ID>
            <wd:ID wd:type=\"Organization_Reference_ID\">8808</wd:ID>
            <wd:ID wd:type=\"Cost_Center_Reference_ID\">8808</wd:ID>
        </wd:Cost_Center>
        <wd:Type wd:Descriptor=\"Cost Center\">
            <wd:ID wd:type=\"WID\">64d1af75258301630e215c7292024500</wd:ID>
            <wd:ID wd:type=\"Organization_Type_ID\">Cost_Center</wd:ID>
        </wd:Type>
        <wd:Reference_ID>8808</wd:Reference_ID>
        <wd:code>8808</wd:code>
        <wd:name>ECO Global Forwarding Austria</wd:name>
        <wd:Included_by_Organizations wd:Descriptor=\"RU7860 Europe Forwarding Overhead\">
            <wd:ID wd:type=\"WID\">b8129574800601751d8538accb023f35</wd:ID>
            <wd:ID wd:type=\"Organization_Reference_ID\">RU7860</wd:ID>
            <wd:ID wd:type=\"Custom_Organization_Reference_ID\">RU7860</wd:ID>
        </wd:Included_by_Organizations>
        <wd:Availability_Date>1900-01-01T00:00:00.000-08:00</wd:Availability_Date>
        <wd:Is_Organization_Active>0</wd:Is_Organization_Active>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Cost_Center wd:Descriptor=\"8810 Sales GF - Austria\">
            <wd:ID wd:type=\"WID\">hello</wd:ID>
            <wd:ID wd:type=\"Organization_Reference_ID\">8810</wd:ID>
            <wd:ID wd:type=\"Cost_Center_Reference_ID\">8810</wd:ID>
        </wd:Cost_Center>
        <wd:Type wd:Descriptor=\"Cost Center\">
            <wd:ID wd:type=\"WID\">6hello</wd:ID>
            <wd:ID wd:type=\"Organization_Type_ID\">Cost_Center</wd:ID>
        </wd:Type>
        <wd:Reference_ID>8810</wd:Reference_ID>
        <wd:code>8810</wd:code>
        <wd:name>Sales GF - Austria</wd:name>
        <wd:Included_by_Organizations wd:Descriptor=\"RU2302 Vienna Global Forwarding\">
            <wd:ID wd:type=\"WID\">b8129574800601968a2e289ccb02062b</wd:ID>
            <wd:ID wd:type=\"Organization_Reference_ID\">RU2302</wd:ID>
            <wd:ID wd:type=\"Custom_Organization_Reference_ID\">RU2302</wd:ID>
        </wd:Included_by_Organizations>
        <wd:Availability_Date>1900-01-01T00:00:00.000-08:00</wd:Availability_Date>
        <wd:Is_Organization_Active>0</wd:Is_Organization_Active>
    </wd:Report_Entry>   

身份证收藏

var orgId = (from x in xml.Descendants(xmlNamespace + "Included_by_Organizations").Elements()
                        select x into y
                        where y.Attribute(xmlNamespace + "type").Value == "Organization_Reference_ID"
                        select y.Value);

为对象属性设置值

var costCenterValues = from cc in xml.Descendants(xmlNamespace + "Report_Entry")
                                   select new CostCenter
                                   {
                                       CostCenterId = cc.Element(xmlNamespace + "Reference_ID").Value,
                                       Name = cc.Element(xmlNamespace + "name").Value,
                                       Code = cc.Element(xmlNamespace + "code").Value,
                                       //CostCenterHierarchyId = 
                                   };

            return costCenterValues.ToList();

鉴于上面的xml,设置CostCenterHierarchyID值的最佳方法是什么?

<wd:Included_by_Organizations wd:Descriptor=\"RU7860 Europe Forwarding Overhead\">
            <wd:ID wd:type=\"WID\">b8129574800601751d8538accb023f35</wd:ID>
            <wd:ID wd:type=\"Organization_Reference_ID\">RU7860</wd:ID>
            <wd:ID wd:type=\"Custom_Organization_Reference_ID\">RU7860</wd:ID>
</wd:Included_by_Organizations>
c# linq
1个回答
0
投票

解决办法

.Element(xmlNamespace + "Included_by_Organizations")
.Elements(xmlNamespace + "ID")
.Where(x => x.Attribute(xmlNamespace + "type").Value == "Organization_Reference_ID")
.FirstOrDefault().Value.ToString()
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.