Quantcast
Channel: Alextv.net feed from blogs.msdn.com
Viewing all articles
Browse latest Browse all 25

Retrieve from RSS Feed using XmlDocument

$
0
0
So I'm trying to wrap up this white paper I'm writing on consuming RSS in Windows Forms. I wrote a whole section on how to apply a custom XSLT transform to a feed, and display the resulting HTML in the WebBrowser. (I gave a less-complete version  of a solution in an earlier blog post.) Then it hit me: "Jay, you fool - what if the RSS feed already has a stylesheet associated with it?"

Fortunately, factoring this into your logic is simple. You can use XmlDocument,  SelectSingleNode(), and a rather funky-looking XPath command to test whether the feed has a stylesheet. If it does, you can pass it straight to the WebBrowser control for rendering, with no further processing required.

            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load("http://blogs.msdn.com/winformsue/rss.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not load XML file. Error: " + ex.Message);
                return;
            }
           
            XmlProcessingInstruction node = (XmlProcessingInstruction)doc.SelectSingleNode("/processing-instruction(\"xml-stylesheet\")");
            if (node != null)
            {
                webBrowser1.Url = new Uri("http://blogs.msdn.com/winformsue/rss.xml");
            }
            else
            {
                // Apply custom style.
            }

Viewing all articles
Browse latest Browse all 25

Trending Articles