简体   繁体   English

XML Reader C#-无法从对象获取所有元素

[英]XML Reader C# - cannot get all elements from object

I am creating a desktop application which pulls in some XML from an eBay RSS feed. 我正在创建一个桌面应用程序,该应用程序从eBay RSS提要中提取一些XML。 I can get the listing title and link but I am unable to get the CurrentPrice element from it. 我可以获得清单标题和链接,但无法从中获取CurrentPrice元素。 I am working in C# using XmlDocument. 我正在使用XmlDocument在C#中工作。

Here is a snippet of the XML file. 这是XML文件的一个片段。

 <rss xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns:e="http://www.ebay.com/marketplace/search/v1/services" version="2.0"> <channel> <cf:listinfo> <cf:group ns="http://www.ebay.com/marketplace/search/v1/services" label="listing format" element="ListingType" data-type="number"/> <cf:group ns="http://www.ebay.com/marketplace/search/v1/services" label="option" element="PaymentMethod" data-type="number"/> <cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="List Order" element="ListOrder" data-type="number"/> <cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="No of bids" element="BidCount" data-type="number"/> <cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="Current auction price" element="CurrentPrice" data-type="number"/> <cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="Buy It Now price" element="BuyItNowPrice" data-type="number"/> <cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="Listing end time" element="ListingEndTime" data-type="number"/> </cf:listinfo> <title> </title> <link>#</link> <subtitle> Customize as you please by changing the URL. The keyword before the .atom / .rss extension determines the result that is displayed </subtitle> <item> <title> Rimmel London Lasting Finish Soft Colour Blush Blusher 020 PINK ROSE </title> <description> <![CDATA[ <table border='0' cellpadding='8'> <tr><td> <a href= 'http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=2&toolid=10039&campid=5338271107&item=264156575558&vectorid=229508&lgeo=1' target='_blank'><img src='http://thumbs3.ebaystatic.com/m/mdy_hoHsem7RVhXXqL-3-ZA/140.jpg' border='0'/></a></td><td><strong>£3.75</strong><br>End Date: Thursday Feb-21-2019 10:28:06 GMT<br>Buy It Now for only: £3.75<br><a href='http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=2&toolid=10039&campid=5338271107&item=264156575558&vectorid=229508&lgeo=1' target='_blank'>Buy It Now</a> | <a href='http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=4&toolid=10039&campid=5338271107&vectorid=229508&lgeo=1&mpre=http%3A%2F%2Fcgi1.ebay.com%2Fws%2FeBayISAPI.dll%3FMfcISAPICommand%3DMakeTrack%26item%3D264156575558%26ssPageName%3DRSS%3AB%3ASRCH%3AUS%3A104' target='_blank'>Add to watch list</a></td></tr> </table> ]]> </description> <pubDate>2019-01-22T10:28:06.000Z</pubDate> <guid>264156575558</guid> <link> http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=2&toolid=10039&campid=5338271107&item=264156575558&vectorid=229508&lgeo=1 </link> <e:EekStatus/> <e:BidCount/> <e:CurrentPrice>3.75</e:CurrentPrice> <e:ListingType>StoreInventory</e:ListingType> <e:BuyItNowPrice/> <e:ListingEndTime>2019-02-21T10:28:06.000Z</e:ListingEndTime> <e:ListOrder>264156575558</e:ListOrder> <e:PaymentMethod>PayPal</e:PaymentMethod> </item> <item> 

The difference is, the elements under the link are prefixed with and I cannot access them. 区别在于,链接下的元素带有前缀,而我无法访问它们。 I have very limited knowledge of XML unfortunately. 不幸的是,我对XML的了解非常有限。

Here is a snippet of the code I am running. 这是我正在运行的代码的片段。

  XmlDocument rssXmlDoc = new XmlDocument(); rssXmlDoc.Load(url); // Parse the Items in the RSS file XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item"); foreach(XmlNode xn in rssNodes) { Console.WriteLine(xn.Name); } StringBuilder rssContent = new StringBuilder(); List<object> ebayList = new List<object>(); // Iterate through the items in the RSS file foreach (XmlNode rssNode in rssNodes) { OleDbConnection conn; conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=E:\\Development\\ebay\\ebay\\ebay.mdb"); conn.Open(); OleDbCommand cmd = conn.CreateCommand(); XmlNode rssSubNode = rssNode.SelectSingleNode("title"); string title = rssSubNode != null ? rssSubNode.InnerText : ""; rssSubNode = rssNode.SelectSingleNode("link"); string link = rssSubNode != null ? rssSubNode.InnerText : ""; rssSubNode = rssNode.SelectSingleNode("description"); string description = rssSubNode != null ? rssSubNode.InnerText : ""; rssSubNode = rssNode.SelectSingleNode("CurrentPrice"); string currentPrice = rssSubNode != null ? rssSubNode.InnerText : ""; Console.Write(currentPrice); 

The other elements return the value perfectly fine - but not CurrentPrice. 其他元素返回的值完全正确-但不是CurrentPrice。

Hope this is enough information for somebody to help me. 希望这对有人有帮助的我有足够的信息。

At my side, it works with the following changes. 在我这方面,它可以进行以下更改。

At the beginning, after 一开始之后

XmlDocument rssXmlDoc = new XmlDocument();      
rssXmlDoc.Load(url);

you add 您添加

XmlNamespaceManager ns = new XmlNamespaceManager(rssXmlDoc.NameTable);
ns.AddNamespace("e", "http://www.ebay.com/marketplace/search/v1/services");

(matches xmlns:e="http://www.ebay.com/marketplace/search/v1/services" ). (与xmlns:e="http://www.ebay.com/marketplace/search/v1/services"匹配)。

The mode reading is done in the following way: 通过以下方式完成模式读取:

rssSubNode = rssNode.SelectSingleNode("e:CurrentPrice", ns);

Explanation: e is a namespace , so CurrentPrice is not the same as e:CurrentPrice . 说明: e是一个名称空间 ,因此CurrentPricee:CurrentPrice In order to explain XmlDocument , which namespace you are expecting, you create a namespace manager and register your namespaces in it. 为了解释XmlDocument ,您需要哪个命名空间,您创建一个命名空间管理器并在其中注册您的命名空间。


Appropriate MS docs entry: https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectsinglenode?view=netframework-4.7.2 适当的MS docs条目: https : //docs.microsoft.com/zh-cn/dotnet/api/system.xml.xmlnode.selectsinglenode? view = netframework- 4.7.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM