Linq to XML C#Descendants:如何读取具有相同名称的属性?

时间:2012-08-08 13:00:04

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

我在网上搜索并找到了很多答案,但他们都使用了特定的课程或以其他方式编写。

我有这个XML响应:

<return xsi:type="ns1:PlentySoapResponse_GetItemsBase">
        <ItemsBase xsi:type="ns1:ArrayOfPlentysoapobject_itembase">
           <item xsi:type="ns1:PlentySoapObject_ItemBase">
              <ItemID xsi:type="xsd:int">2</ItemID>
              <ItemNo xsi:type="xsd:string"/>
               ............. 
              <DeepLink xsi:type="xsd:string">/a-2/</DeepLink>
           </item>
           <item xsi:type="ns1:PlentySoapObject_ItemBase">
              <ItemID xsi:type="xsd:int">4</ItemID>
              <ItemNo xsi:type="xsd:string"/>

我这样读了:

XDocument loaded = XDocument.Parse(readString);
            for (int i = 0; i < 50; i++)
            {
            var items = from x in loaded.Descendants("return")
                                select new 
                                {
                                    ItemID = x.Descendants("ItemID").First().Value,
                                    Name = x.Descendants("Name").First().Value,
                                    EKnet = x.Descendants("PurchasePriceNet").First().Value
                                };

                foreach (var x in items)
                {
                    item[i, 0] = x.ItemID.ToString();
                    item[i, 1] = x.Name.ToString();
                    item[i, 2] = x.EKnet.ToString();
                }
            }
        else
        {
            MessageBox.Show("Wrong XML Call");
        }
        return item;
      }

但我总是得到相同的物品属性。<​​/ p>

有没有简单的方法来获取下一个项目属性? 我读了一个“IEnumerable”的例子,但这总是使用一个我不想使用的特定类。

非常感谢提前。

编辑:

<SOAP-ENV:Body>
  <ns1:GetItemsBaseResponse>  
     <return xsi:type="ns1:PlentySoapResponse_GetItemsBase">
        <ItemsBase xsi:type="ns1:ArrayOfPlentysoapobject_itembase">
           <item xsi:type="ns1:PlentySoapObject_ItemBase">
              <ItemID xsi:type="xsd:int">2</ItemID>
              <ItemNo xsi:type="xsd:string"/>
              <ExternalItemID xsi:type="xsd:string"/>
              <EAN1 xsi:type="xsd:string">0023272005641</EAN1>
              <EAN2 xsi:type="xsd:string"/>
              <EAN3 xsi:type="xsd:string"/>
              <EAN4 xsi:type="xsd:string"/>
              <ISBN xsi:type="xsd:string"/>
              <ASIN xsi:type="xsd:string">B000NTAG66</ASIN>
              <Type xsi:type="xsd:int">0</Type>
              <Model xsi:type="xsd:string"/>
              <ProducerID xsi:type="xsd:int">0</ProducerID>
              <VATInternalID xsi:type="xsd:int">0</VATInternalID>
              <Marking1ID xsi:type="xsd:int">0</Marking1ID>
              <Marking2ID xsi:type="xsd:int">11</Marking2ID>
              <CustomsTariffNumber xsi:type="xsd:string"/>
              <FSK xsi:type="xsd:int">0</FSK>
              <Condition xsi:type="xsd:int">0</Condition>
              <Position xsi:type="xsd:string"/>
              <StorageLocation xsi:type="xsd:int">0</StorageLocation>
              <WebShopSpecial xsi:type="xsd:string">0</WebShopSpecial>
              <Published xsi:type="xsd:int">0</Published>
              <LastUpdate xsi:type="xsd:int">1344392167</LastUpdate>
              <ItemURL xsi:nil="true"/>
              <EbayEPID xsi:type="xsd:string"/>
              <ParcelServicePresetIDs xsi:type="ns1:ArrayOfPlentysoapobject_integer">
                 <item xsi:type="ns1:PlentySoapObject_Integer">
                    <intValue xsi:type="xsd:int">21</intValue>
                 </item>
              </ParcelServicePresetIDs>
              <ProducingCountryID xsi:type="xsd:int">1</ProducingCountryID>
              <FreeTextFields xsi:type="ns1:PlentySoapObject_ItemFreeTextFields">
                 <Free1 xsi:type="xsd:string"/>
                 <Free2 xsi:type="xsd:string"><![CDATA[http://games.shop.ebay.de/PC-Videospiele-/1249/i.html?LH_BIN=1&LH_ItemCondition=11&Plattform=Xbox%2520360&_trkparms=65%253A12%257C66%253A2%257C39%253A1%257C72%253A4674&rt=nc&_nkw=star+wars+force+unleashed&_catref=1&_dmpt=de_entertainment_games&_trksid=p3286.c0.m14&_sop=15&_sc=1]]></Free2>
                 <Free3 xsi:type="xsd:string"/>
                 <Free4 xsi:type="xsd:string">1</Free4>
                 <Free5 xsi:type="xsd:string">UK-Import: Spiel in Deutsch, Anleitung in Englisch</Free5>
                 <Free6 xsi:type="xsd:string">Actionspiele</Free6>
                 <Free7 xsi:type="xsd:string">XBOX 360</Free7>
                 <Free8 xsi:type="xsd:string">ages_12_and_over</Free8>
                 <Free9 xsi:type="xsd:string"/>
                 <Free10 xsi:type="xsd:string">1</Free10>
                 <Free11 xsi:type="xsd:string">0</Free11>
                 <Free12 xsi:nil="true"/>
                 <Free13 xsi:nil="true"/>
                 <Free14 xsi:nil="true"/>
                 <Free15 xsi:nil="true"/>
                 <Free16 xsi:nil="true"/>
                 <Free17 xsi:nil="true"/>
                 <Free18 xsi:nil="true"/>
                 <Free19 xsi:nil="true"/>
                 <Free20 xsi:nil="true"/>
              </FreeTextFields>
              <Texts xsi:type="ns1:PlentySoapObject_ItemTexts">
                 <Lang xsi:type="xsd:string">de</Lang>
                 <Name xsi:type="xsd:string">Star Wars: The Force Unleashed 1 - Xbox 360</Name>
                 <Name2 xsi:type="xsd:string"/>
                 <Name3 xsi:type="xsd:string"/>
                 <ShortDescription xsi:nil="true"/>
                 <LongDescription xsi:nil="true"/>
                 <TechnicalData xsi:nil="true"/>
                 <MetaDescription xsi:nil="true"/>
                 <Keywords xsi:type="xsd:string"/>
              </Texts>
              <PriceSet xsi:type="ns1:PlentySoapObject_ItemPriceSet">
                 <PriceID xsi:type="xsd:int">2</PriceID>
                 <Lot xsi:type="xsd:float">0</Lot>
                 <Unit xsi:type="xsd:string">C62</Unit>
                 <Unit1 xsi:type="xsd:string"/>
                 <Unit2 xsi:type="xsd:string"/>
                 <PackagingUnit xsi:type="xsd:string">1</PackagingUnit>
                 <Price xsi:type="xsd:float">20.99</Price>
                 <Price1 xsi:type="xsd:float">18.65</Price1>
                 <Price2 xsi:type="xsd:float">23.99</Price2>
                 <Price3 xsi:type="xsd:float">999</Price3>
                 <Price4 xsi:type="xsd:float">999</Price4>
                 <Price5 xsi:type="xsd:float">0</Price5>
                 <Price6 xsi:type="xsd:float">0</Price6>
                 <Price7 xsi:type="xsd:float">0</Price7>
                 <Price8 xsi:type="xsd:float">0</Price8>
                 <Price9 xsi:type="xsd:float">0</Price9>
                 <Price10 xsi:type="xsd:float">0</Price10>
                 <Price11 xsi:type="xsd:float">23.7</Price11>
                 <Price12 xsi:type="xsd:float">33.7</Price12>
                 <RRP xsi:type="xsd:float">0</RRP>
                 <VAT xsi:type="xsd:float">19</VAT>
                 <WeightInGramm xsi:type="xsd:int">0</WeightInGramm>
                 <WidthInMM xsi:type="xsd:int">0</WidthInMM>
                 <LengthInMM xsi:type="xsd:int">0</LengthInMM>
                 <HeightInMM xsi:type="xsd:int">0</HeightInMM>
                 <Position xsi:type="xsd:int">0</Position>
                 <ShowOnly xsi:type="xsd:int">0</ShowOnly>
                 <RebateLevelPrice6 xsi:type="xsd:int">0</RebateLevelPrice6>
                 <RebateLevelPrice7 xsi:type="xsd:int">0</RebateLevelPrice7>
                 <RebateLevelPrice8 xsi:type="xsd:int">0</RebateLevelPrice8>
                 <PurchasePriceNet xsi:type="xsd:float">14.38</PurchasePriceNet>
                 <Package xsi:type="xsd:int">0</Package>
                 <TypeOfPackage xsi:type="xsd:int">0</TypeOfPackage>
                 <UnitLoadDevice xsi:type="xsd:int">0</UnitLoadDevice>
              </PriceSet>
              <Stock xsi:type="ns1:PlentySoapObject_ItemStock">
                 <Limitation xsi:type="xsd:int">1</Limitation>
                 <MainWarehouseID xsi:type="xsd:int">2</MainWarehouseID>
                 <WebshopVisiblePositiveStock xsi:type="xsd:boolean">false</WebshopVisiblePositiveStock>
                 <WebshopInvisibleNoStock xsi:type="xsd:boolean">false</WebshopInvisibleNoStock>
                 <ChangeAvailablePositiveStock xsi:type="xsd:boolean">true</ChangeAvailablePositiveStock>
                 <ChangeNotAvailableNoStock xsi:type="xsd:boolean">true</ChangeNotAvailableNoStock>
                 <ChangeAvailablePositiveStockVariant xsi:type="xsd:boolean">false</ChangeAvailablePositiveStockVariant>
                 <ChangeNotAvailableNoStockVariant xsi:type="xsd:boolean">false</ChangeNotAvailableNoStockVariant>
                 <StorageLocation xsi:nil="true"/>
              </Stock>
              <Availability xsi:type="ns1:PlentySoapObject_ItemAvailability">
                 <AvailabilityID xsi:type="xsd:int">5</AvailabilityID>
                 <Webshop xsi:type="xsd:int">1</Webshop>
                 <WebAPI xsi:type="xsd:int">1</WebAPI>
                 <Inactive xsi:type="xsd:int">0</Inactive>
                 <AmazonFBA xsi:type="xsd:int">1</AmazonFBA>
                 <AmazonFEDAS xsi:type="xsd:string"/>
                 <AmazonProduct xsi:type="xsd:int">31</AmazonProduct>
                 <Gimahhot xsi:type="xsd:int">0</Gimahhot>
                 <GoogleBase xsi:type="xsd:int">0</GoogleBase>
                 <Hitmeister xsi:type="xsd:int">0</Hitmeister>
                 <MeinPaket xsi:type="xsd:int">0</MeinPaket>
                 <Mercateo xsi:type="xsd:int">0</Mercateo>
                 <Quelle xsi:type="xsd:int">0</Quelle>
                 <Moebelprofi xsi:type="xsd:int">0</Moebelprofi>
                 <Zalando xsi:type="xsd:int">0</Zalando>
                 <Restposten xsi:type="xsd:int">0</Restposten>
                 <Shopgate xsi:type="xsd:int">0</Shopgate>
                 <Shopperella xsi:type="xsd:int">0</Shopperella>
                 <ShopShare xsi:type="xsd:int">0</ShopShare>
                 <Tradoria xsi:type="xsd:int">0</Tradoria>
                 <TradoriaCategory xsi:type="xsd:int">0</TradoriaCategory>
                 <Yatego xsi:type="xsd:int">0</Yatego>
                 <AvailableUntil xsi:type="xsd:int">0</AvailableUntil>
                 <MaximumSalesOrderQuantity xsi:type="xsd:int">0</MaximumSalesOrderQuantity>
                 <Neckermann xsi:type="xsd:int">0</Neckermann>
              </Availability>
              <Others xsi:nil="true"/>
              <Categories xsi:nil="true"/>
              <AttributeValueSets xsi:nil="true"/>
              <ItemSuppliers xsi:nil="true"/>
              <ItemProperties xsi:nil="true"/>
              <ItemAttributeMarkup xsi:nil="true"/>
              <BundleType xsi:nil="true"/>
              <DeepLink xsi:type="xsd:string">/a-2/</DeepLink>
           </item>

2 个答案:

答案 0 :(得分:2)

试试这个:

        XDocument loaded = XDocument.Parse(readString);
        var items = loaded
                    .Descendants("return")
                    .FirstOrDefault()
                    .Element("ItemsBase")
                    .Elements("item");
        int i = 0;
        foreach (var _item in items)
        {
            item[i, 0] = _item.Element("ItemID").Value;
            item[i, 1] = _item.Element("Texts").Element("Name").Value;
            item[i, 2] = _item.Element("PriceSet").Element("PurchasePriceNet").Value;
            i++;
        }

答案 1 :(得分:0)

XDocument xdoc = XDocument.Parse(xml);
        var items = from ret in xdoc.Descendants("return")
                    from itb in ret.Descendants("ItemsBase")
                    from item in itb.Descendants("item")
                    select new {item};

        foreach (var item in items)
        {
            // do what you want with item,
        }