0

xml で Webservice 応答を取得しました。

<Items>
  <Item>
    <SmallImage>
      <Url>http://xxx<url>
      <height></height>
      <weight></weight>
    </SmallImage>
    <LargeImage>
      <Url>http://xxx<url>
      <height></height>
      <weight></weight>
    </LargeImage>
    <ItemAttributes>
      <Binding>Apparel</Binding>
      <Brand>Calvin Klein Jeans</Brand>
      <Department>mens</Department>
      <Title>Calvin Klein Jeans Men's Rusted Antique Skinny Jean</Title>
    </ItemAttributes>
    <SimilarProducts>
       <SimilarProduct>
         <ASIN>B0018QK10E</ASIN>
         <Title>New Balance Men's M574 Sneaker</Title>
       </SimilarProduct>
    </SimilarProducts>
  </Item>
</Items>

ここでは、タイトルを表示する必要があります。アイテム->アイテム->アイテム属性->タイトル

私はこのようにしてみました。

           #region Product Title
        var Title = xd.Descendants(ns + "Item").Select(b => new
        {
            PTitle = b.Element(ns + "ItemAttributes").Element(ns + "Title").Value
        }).ToList();
        #endregion 

しかし、それは Object null を返します。さらに情報が必要な場合はお知らせください。前もって感謝します。

4

2 に答える 2

0

解決:

var Title = xd.Descendants(ns + "Items").Elements(ns + "Item").Select(BTitle => BTitle.Elements(ns + "ItemAttributes").Select(BTitle1 => (string)BTitle1.Element(ns + "Title")).FirstOrDefault() ?? "Null").ToList();
于 2012-10-06T10:51:27.740 に答える
0

LINQ クエリで要素を検索するには、適切な xml 名前空間名が必要です。

xmlnamespace を取得できます。

XNamespace ns = xd.Root.Attribute("xmlns").Value;

LINQ クエリで ns を使用します。

または試してみてください。

var Items = xd.Descendants().Where(a => a.Name.LocalName == "Item");
var ItemAttributes = Items.Descendants().Where(b => b.Name.LocalName == "ItemAttributes");
List<string> Titles = ItemAttributes.Descendants().Where(c => c.Name.LocalName == "Title").Select(o => o.Value).ToList<string>();
于 2012-09-12T17:10:12.267 に答える