Linq到XML从元素中获取特定的值

本文关键字:获取 XML 元素 Linq | 更新日期: 2023-09-27 18:05:01

我正在寻找<ConvertedCurrentPrice currencyID="USD">100.0</ConvertedCurrentPrice>下的100.0的值

var price = from v in doc.Elements("Item").Elements("ConvertedCurrentPrice").Single()

但不工作

XML:

  <GetSingleItemResponse xmlns="urn:ebay:apis:eBLBaseComponents">
      <Timestamp>2015-07-26T19:39:05.104Z</Timestamp>
      <Ack>Warning</Ack>
      <Errors>
        <ShortMessage>Version specified is above current version.</ShortMessage>
        <LongMessage>Version specified is above current version.  Setting version to current release version.</LongMessage>
        <ErrorCode>1.30</ErrorCode>
        <SeverityCode>Warning</SeverityCode>
        <ErrorClassification>RequestError</ErrorClassification>
      </Errors>
      <Build>E897_CORE_APILW_17290128_R1</Build>
      <Version>897</Version>
      <Item>
        <ItemID>221373860717</ItemID>
        <EndTime>2015-08-04T19:26:15.000Z</EndTime>
        <ViewItemURLForNaturalSearch>http://www.ebay.com/itm/Hello-Kitty-Wind-Up-60-Minute-Kitchen-Timer-3-5-/221371860717</ViewItemURLForNaturalSearch>
        <ListingType>FixedPriceItem</ListingType>
        <Location>Trenton, New Jersey</Location>
        <GalleryURL>http://thumbs2.ebaystatic.com/pict/2213718607178080_1.jpg</GalleryURL>
        <PictureURL>http://i.ebayimg.com/00/s/ODg5WDYxMA==/z/6u0AAOxydlFS-Sez/$_1.JPG?set_id=880000500F</PictureURL>
        <PrimaryCategoryID>98852</PrimaryCategoryID>
        <PrimaryCategoryName>Home &amp; Garden:Kitchen, Dining &amp; Bar:Kitchen Tools &amp; Gadgets:Timers</PrimaryCategoryName>
        <BidCount>0</BidCount>
        <ConvertedCurrentPrice currencyID="USD">100.0</ConvertedCurrentPrice>
        <ListingStatus>Active</ListingStatus>
        <TimeLeft>P8DT23H47M10S</TimeLeft>
        <Title>Hello Kitty Wind Up 60 Minute Kitchen Timer-3.5"</Title>
        <Country>US</Country>
        <AutoPay>true</AutoPay>
        <ConditionID>1000</ConditionID>
        <ConditionDisplayName>New</ConditionDisplayName>
      </Item>
    </GetSingleItemResponse>

Linq到XML从元素中获取特定的值

你需要修复一些bug

XNamespace ns = "urn:ebay:apis:eBLBaseComponents";
var price = doc.Root.Elements(ns + "Item")
                    .Elements(ns + "ConvertedCurrentPrice")
                    .Single()
                    .Value;
相关文章: