1

Here is my Code?

  XmlDocument rssXmlDoc = new XmlDocument();

           // Load the RSS file from the RSS URL
           rssXmlDoc.Load("https://polsky.uchicago.edu/events/feed/");


                var nsmgr = new XmlNamespaceManager(rssXmlDoc.NameTable);
                nsmgr.AddNamespace("event", "http://www.w3.org/1999/XSL;Transform");

            // Parse the Items in the RSS file
            XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item", nsmgr);

I know that the XML has some elements that contain "&", and I also know that it is really not up to me to fix this bad RSS feed; however, I am not certain if they will comply. Is there anything I can do?

The following exception is thrown:

An error occurred while parsing EntityName. Line 138, position 26.

Paul T. Rykiel
  • 1,177
  • 6
  • 26
  • 48

2 Answers2

2

You can't fix that with an XML parser because it's invalid XML. & isn't allowed without being escaped.

You can however read in the bad XML as a string, do a string replace of & for &, then process the string with your normal XML parser.

You can also bracket it in CDATA and get on with your life 8-)

PS. If you go with the first method, be sure to check for and handle the other "bad" characters like <>"' (less than, greater than, double quote, single quote)

Terry Carmen
  • 3,720
  • 1
  • 16
  • 32
1

I use System.Security.SecurityElement.Escape() to take care of "XML encoding" requirements. It works essentially the same as the System.Web.HttpUtility.HtmlEncode Encode/decode

https://learn.microsoft.com/en-us/dotnet/api/system.security.securityelement.escape

egray
  • 390
  • 1
  • 4