XML Reader C#-无法从对象

时间:2019-02-07 15:03:09

标签: c# xml rss

我正在创建一个桌面应用程序,该应用程序从eBay RSS提要中提取一些XML。我可以获得清单标题和链接,但无法从中获取CurrentPrice元素。我正在使用XmlDocument在C#中工作。

这是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>

区别在于,链接下的元素带有前缀,而我无法访问它们。不幸的是,我对XML的了解非常有限。

这是我正在运行的代码的片段。

 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);

其他元素可以完美返回值-但不能返回CurrentPrice。

希望这对于有人可以帮助我的信息足够。

1 个答案:

答案 0 :(得分:2)

在我这方面,它可以进行以下更改。

在一开始,之后

action-dispatch

您添加

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

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

通过以下方式完成模式读取:

xmlns:e="http://www.ebay.com/marketplace/search/v1/services"

说明:rssSubNode = rssNode.SelectSingleNode("e:CurrentPrice", ns); 命名空间,因此eCurrentPrice不同。为了解释e:CurrentPrice(您期望使用哪个名称空间),您需要创建一个名称空间管理器并在其中注册您的名称空间。


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