Mike
Mike

Reputation: 251

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. I can get the listing title and link but I am unable to get the CurrentPrice element from it. I am working in C# using XmlDocument.

Here is a snippet of the XML file.

<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.

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.

Hope this is enough information for somebody to help me.

Upvotes: 1

Views: 152

Answers (1)

Vlad
Vlad

Reputation: 35594

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").

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. In order to explain XmlDocument, which namespace you are expecting, you create a namespace manager and register your namespaces in it.


Appropriate MS docs entry: https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectsinglenode?view=netframework-4.7.2

Upvotes: 2

Related Questions