XmlReader throws on an RSS feed, when the RSS document includes an embedded <script> block.
Posted
by Cheeso
on Stack Overflow
See other posts from Stack Overflow
or by Cheeso
Published on 2010-05-12T02:09:58Z
Indexed on
2010/05/12
2:14 UTC
Read the original article
Hit count: 589
The code:
using (XmlReader xmlr = XmlReader.Create(new StringReader(allXml)))
{
var items = from item in SyndicationFeed.Load(xmlr).Items
select item;
}
The exception:
Exception: System.Xml.XmlException: Unexpected node type Element.
ReadElementString method can only be called on elements with simple or empty content. Line 11, position 25.
at System.Xml.XmlReader.ReadElementString()
at System.ServiceModel.Syndication.Rss20FeedFormatter.ReadXml(XmlReader reader, SyndicationFeed result)
at System.ServiceModel.Syndication.Rss20FeedFormatter.ReadFeed(XmlReader reader)
at System.ServiceModel.Syndication.Rss20FeedFormatter.ReadFrom(XmlReader reader)
at System.ServiceModel.Syndication.SyndicationFeed.Load[TSyndicationFeed](XmlReader reader)
at System.ServiceModel.Syndication.SyndicationFeed.Load(XmlReader reader)
at Ionic.ToolsAndTests.ReadRss.Run() in c:\dev\dotnet\ReadRss.cs:line 90
The XML content:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://www.ibm.com/developerworks/mydeveloperworks/blogs/roller-ui/styles/rss.xsl" media="screen"?><rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
<title>Software architecture, software engineering, and Renaissance Jazz</title>
<link>https://www.ibm.com/developerworks/mydeveloperworks/blogs/gradybooch</link>
<atom:link rel="self" type="application/rss+xml" href="https://www.ibm.com/developerworks/mydeveloperworks/blogs/gradybooch/feed/entries/rss?lang=en" />
<description>Software architecture, software engineering, and Renaissance Jazz</description>
<language>en-us</language>
<copyright>Copyright <script type='text/javascript'> document.write(blogsDate.date.localize (1273534889181));</script></copyright>
<lastBuildDate>Mon, 10 May 2010 19:41:29 -0400</lastBuildDate>
As you can see, on line 11, at position 25, there's a script block inside the <copyright>
element.
Other people have reported similar errors with other XML documents.
The way I worked around this was to do a StreamReader.ReadToEnd, then do Regex.Replace on the result of that to yank out the script block, before passing the modified string to XmlReader.Create(). Feels like a hack.
Has anyone got a better approach? I don't like this because I have to read in a 125k string into memory.
Is it valid rss to include "complex content" like that - a script block inside an element?
Thanks.
© Stack Overflow or respective owner