xyドキュメントのより深いコンテナから値を取得しようとしていますが、常にNull参照で実行されます。XML文書をIEnumerable <XElement>に変換し、値を取得する
これは私のコードは、これまでのところです:
StreamReader xmlStream = new StreamReader(rsp.GetResponseStream());
string XMLstr = xmlStream.ReadToEnd();
XDocument xelement = XDocument.Parse(XMLstr);
xmlStream.Close();
IEnumerable<XElement> items = xelement.Elements();
foreach (var item in items)
{
Console.WriteLine(item.Element("ItemID").Value.ToString());
}
これは私が一緒に仕事をしたいXML:
<GetSellerListResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2016-02-26T22:02:38.968Z</Timestamp>
<Ack>Success</Ack>
<Version>967</Version>
<Build>967_CORE_BUNDLED_10708779_R1</Build>
<PaginationResult>
<TotalNumberOfPages>14</TotalNumberOfPages>
<TotalNumberOfEntries>27</TotalNumberOfEntries>
</PaginationResult>
<HasMoreItems>true</HasMoreItems>
<ItemArray>
<Item>
<AutoPay>false</AutoPay>
<BuyerProtection>ItemIneligible</BuyerProtection>
<Country>US</Country>
<Currency>USD</Currency>
<HitCounter>NoHitCounter</HitCounter>
<ItemID>110043597553</ItemID>
<ListingDetails>
<StartTime>2016-02-12T23:35:27.000Z</StartTime>
<EndTime>2016-02-19T23:35:27.000Z</EndTime>
<ViewItemURL>http://cgi.sandbox.ebay.com/ws/eBayISAPI.dll?ViewItem&
item=110043597553&category=41393</ViewItemURL>
<HasUnansweredQuestions>false</HasUnansweredQuestions>
<HasPublicMessages>false</HasPublicMessages>
<BuyItNowAvailable>true</BuyItNowAvailable>
</ListingDetails>
<ListingDuration>Days_7</ListingDuration>
<Location>Santa Cruz, California</Location>
<PrimaryCategory>
<CategoryID>41393</CategoryID>
<CategoryName>Collectibles:Decorative Collectibles:Other</CategoryName>
</PrimaryCategory>
<Quantity>1</Quantity>
<ReviseStatus>
<ItemRevised>false</ItemRevised>
</ReviseStatus>
<SecondaryCategory>
<CategoryID>95116</CategoryID>
<CategoryName>Collectibles:Disneyana:Contemporary (1968-Now):Bobblehead Figures</CategoryName>
</SecondaryCategory>
<SellingStatus>
<BidCount>0</BidCount>
<BidIncrement currencyID="USD">0.5</BidIncrement>
<ConvertedCurrentPrice currencyID="USD">11.49</ConvertedCurrentPrice>
<CurrentPrice currencyID="USD">11.49</CurrentPrice>
<MinimumToBid currencyID="USD">11.49</MinimumToBid>
<QuantitySold>0</QuantitySold>
<SecondChanceEligible>false</SecondChanceEligible>
<ListingStatus>Completed</ListingStatus>
</SellingStatus>
<ShipToLocations>US</ShipToLocations>
<Site>US</Site>
<Storefront>
<StoreCategoryID>1</StoreCategoryID>
<StoreCategory2ID>0</StoreCategory2ID>
<StoreURL>http://www.stores.sandbox.ebay.com/id=132854966</StoreURL>
</Storefront>
<SubTitle>Micky, with the ears!</SubTitle>
<TimeLeft>PT0S</TimeLeft>
<Title>Kelly's Kitsch</Title>
<WatchCount>0</WatchCount>
<PostalCode>95062</PostalCode>
<PictureDetails>
<GalleryURL>http://thumbs.ebaystatic.com/pict/41007087008080_0.jpg</GalleryURL>
<PhotoDisplay>None</PhotoDisplay>
<PictureURL>http://thumbs.ebaystatic.com/pict/41007087008080_0.jpg</PictureURL>
</PictureDetails>
<ProxyItem>false</ProxyItem>
<ReturnPolicy>
<RefundOption>MoneyBack</RefundOption>
<Refund>Money Back</Refund>
<ReturnsWithinOption>Days_30</ReturnsWithinOption>
<ReturnsWithin>30 Days</ReturnsWithin>
<ReturnsAcceptedOption>ReturnsAccepted</ReturnsAcceptedOption>
<ReturnsAccepted>Returns Accepted</ReturnsAccepted>
<Description>Returns accepted only if item is not as described.</Description>
<ShippingCostPaidByOption>Buyer</ShippingCostPaidByOption>
<ShippingCostPaidBy>Buyer</ShippingCostPaidBy>
</ReturnPolicy>
<PaymentAllowedSite>US</PaymentAllowedSite>
</Item>
</ItemArray>
<ItemsPerPage>2</ItemsPerPage>
<PageNumber>1</PageNumber>
<ReturnedItemCountActual>2</ReturnedItemCountActual>
</GetSellerListResponse>
は、誰もが私が行方不明ですか説明していただけますか?それはちょうどは、ルート要素を返すために起こっているので
おかげ
それらを見て
そして最後に置かれている場所のアイテムIDのすべてを選択します。私はコードを完全に乱してしまった。私は最初のコードを試しました。しかし、ただ一つの要素を返すだけです。より多くの「アイテム」コンテナがある場合はどうなりますか? –
@josef_skywalker:それは何度も繰り返すでしょう。しかし、あなたが与えたサンプルXMLは1つのItem要素しか持っていません。 –
もう一度...あなたは正しいです。あなたに感謝します! –