Jaa


Retrieve from RSS Feed using XmlDocument

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("https://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("https://blogs.msdn.com/winformsue/rss.xml");
}
else
{
// Apply custom style.
}