2016-08-23 73 views
0

我的問題是,我似乎無法獲得我需要的節點列表。我嘗試了多種解決方案,但似乎並不奏效。這是我的xml文件的一部分:在XML中找不到某個節點

<findItemsByCategoryResponse xmlns="http://www.ebay.com/marketplace/search/v1/services"> 
    <ack>Success</ack> 
    <version>1.13.0</version> 
    <timestamp>2016-08-23T07:33:22.497Z</timestamp> 
    <searchResult count="100"> 
     <item> 
      <itemId>152210133431</itemId> 
      <title>...</title> 
      <globalId>EBAY-GB</globalId> 
      <primaryCategory> 
       <categoryId>218</categoryId> 
       <categoryName>Magic the Gathering</categoryName> 
      </primaryCategory> 
      <galleryURL> 
http://thumbs4.ebaystatic.com/m/meIDrVqhmbpQMYCxzeUvR9Q/140.jpg 
</galleryURL> 
      <viewItemURL> 
http://www.ebay.co.uk/itm/MTG-See-Unwritten-Khans-Tarkir-MYTHIC-MINT-/152210133431 
</viewItemURL> 
      <paymentMethod>PayPal</paymentMethod> 
      <autoPay>false</autoPay> 
      <location>London,United Kingdom</location> 
      <country>GB</country> 
      <shippingInfo> 
       <shippingServiceCost currencyId="GBP">1.1</shippingServiceCost> 
       <shippingType>Flat</shippingType> 
       <shipToLocations>GB</shipToLocations> 
      </shippingInfo> 
      <sellingStatus> 
       <currentPrice currencyId="GBP">0.5</currentPrice> 
       <convertedCurrentPrice currencyId="GBP">0.5</convertedCurrentPrice> 
       <bidCount>0</bidCount> 
       <sellingState>Active</sellingState> 
       <timeLeft>P0DT0H19M12S</timeLeft> 
      </sellingStatus> 
      <listingInfo> 
       <bestOfferEnabled>false</bestOfferEnabled> 
       <buyItNowAvailable>false</buyItNowAvailable> 
       <startTime>2016-08-18T07:52:34.000Z</startTime> 
       <endTime>2016-08-23T07:52:34.000Z</endTime> 
       <listingType>Auction</listingType> 
       <gift>false</gift> 
      </listingInfo> 
      <condition> 
       <conditionId>1000</conditionId> 
       <conditionDisplayName>New</conditionDisplayName> 
      </condition> 
      <isMultiVariationListing>false</isMultiVariationListing> 
      <topRatedListing>false</topRatedListing> 
     </item> 
    </searchResult> 
    <paginationOutput>...</paginationOutput> 
    <itemSearchURL>...</itemSearchURL> 
</findItemsByCategoryResponse> 

通常有這xml文件100個節點,我只是短的文件。我試圖在XmlNodeList中獲取''item''節點和一切內容,但是當我調試它時說它包含0個項目。在此之後,我想要檢索一些數據,如代碼中所示。注意:我嘗試了很多xpath值! 這裏是我使用的代碼:

  XmlDocument xmlText = new XmlDocument(); 
      xmlText.Load(basicUrl); 
      XmlElement root = xmlText.DocumentElement; 
      XmlNodeList xnList = root.SelectNodes("//item"); 
      foreach(XmlNode item in xnList) 
      { 
       string title = item["title"].InnerText; 
       Console.WriteLine(title); 
      } 

回答

1

XPath表達式始終具有感知名稱空間(如果該元素有一個命名空間,則必須的XPath通過命名空間引用它)。另外,在XPath表達式中,「默認」命名空間總是在URI「」中。在你的情況,你應該做這樣的事情:

 XmlDocument xmlText = new XmlDocument(); 
     xmlText.Load(yourXml); 
     XmlElement root = xmlText.DocumentElement; 
     XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlText.NameTable); 
     nsmgr.AddNamespace("t", "http://www.ebay.com/marketplace/search/v1/services"); 
     XmlNodeList xnList = xmlText.SelectNodes("//t:item", nsmgr); 
     foreach (XmlNode item in xnList) 
     { 
      string title = item["title"].InnerText; 
      Console.WriteLine(title); 
     } 

的SelectNodes命名空間:

https://msdn.microsoft.com/en-US/library/4bektfx9(v=vs.110).aspx

+0

如何解釋_why_這將/能夠幫助? –

+0

不太明白,它似乎不起作用,你能給我多一點信息?或者可能用linq而不是Xpath來搜索它? – Wouter

+0

Xpath是一種在XML中進行搜索的非常好的方法我編輯了我的答案(我使用問題中提供的xml測試了代碼) –