2016-08-23 48 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测试了代码) –