RSS Feeds and DataSets

So as I write this at 1:10 in the morning, I have just finished beating my head against a fairly hard wall.  The task was to load an RSS Feed into a DataSet.  Then once in the DataSet, the feed could be bound to a Datagrid, a DataRepeater, etc.  The plan was simple.  The strategy sound. Or so I thought.

"No plan survives contact with the enemy." -Field Marshal Helmuth von Moltke.

While I suspect that many of you are aware of the quote, it´s place in the process of strategic is frequently forgotten.  The quote is intended to remind strategists to set broad objectives and seize unexpected opportunities when they arise. Of course, in this instance, I´m using the more pessimistic “stuff happens“ meaning that is more commonly ascribed to the quote.

So I naively start the process by creating a DataSet object and using the ReadXml method to build it.  Due to the foresight of the .NET Framework designers, I can simply pass the URL to the RSS Feed as a parameter.  Sweet.

But then I run into a snag. Or, to put it another way, Moltke proved his prescience. Within ReadXml, a DuplicateNameException is thrown. The specific message was  “A column named 'comments' already belongs to this DataTable.“ After an examination of the RSS feed, I discovered that the Slash RSS module uses a Comments tag. In the RSS feed, it is properly namespaced, but the namespace is not recognized by ReadXml.  So the Comments tag in RSS clashes with the Comments tag in Slash RSS.

The solution to this problem is not as clean as I would have liked.  In my ideal world, there would be a way to limit the namespaces that are loaded into the DataSet.  Perhaps using the XmlNamespaceManager, for instance.  But for all the Googling that I did, there doesn't appear to be any solution down this alley. So instead I turned to a kludgey (from my perspective) method that involves transforming the RSS feed using XSL.

My next problem immediately followed this solution.  When I processed the transformed RSS feed, another exception was thrown.  This time it was a ArgumentException that read "The same table (p) cannot be the child table in two nested relations.". Back to the RSS feed we went.  What we saw was that if the post was XHTML compliant, then a separate <Body> block was contained within the post.  In this <Body> block was the post complete with the various markup tags (like <p>) intact.  This differs from the normal format of the post, where the angle brackets surrounding the tag are converted the &lt; and &gt;. The result was that ReadXml was choking on the fact that <P> existed in to separate items.

Back to the XSL, where I excluded the <Body> block from the RSS feed.  Now my XSL file looks like the following:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"   
     xmlns
:slash="http://purl.org/rss/1.0/modules/slash/" 
     xmlns
:xhtml="http://www.w3.org/1999/xhtml" >

    <xsl:template match="*|@*|comment()|text()">
       
<xsl:copy>
           
<xsl:apply-templates select="*|@*|comment()|text()"/>
       
</xsl:copy>
    </
xsl:template>

    <xsl:template match="slash:comments">
        <SlashComments>
            <
xsl:apply-templates select="*|@*|comment()|text()"/>
        </
SlashComments>
    </
xsl:template>

    <xsl:template match="xhtml:body"></xsl:template>

</xsl:stylesheet>

Mission accomplished.  I now have a DataSet that contains the necessary information from an RSS Feed.  Next up is to actually bind it to the desired control.  Haven't gotten around to it yet, but I'm hoping that it is much easier.  It certainly shouldn't be much more challenging.

Comments

  • bruce October 19, 2004 7:25 PM

    hi,
    I have to read xml documents in to a dataset but the following error comes:
    The same table (Role) cannot be the child table in two nested relations.
    I can not change the xml document as i have to do this for a number of a number of xml documents on a regular basis.
    Is there any way i can read xml document with a bit of programming that the above error does not come.

  • bruce October 19, 2004 7:51 PM

    The problem occurs when the XML document contains the same tag within two separate parents. For example

    <First>
    <Item>A</Item>
    </First>
    <Second>
    <Item>A</Item>
    </Second>

    When loading the XML document, only one Item table is created and it can't be subordinate to both the First and Second tables. The easiest solution is to convert the XML document so that the interior nodes are not duplicated. Since you have to do this to multiple document, you might consider using an XSLT doc that will automate the conversion.

    Hope that helps.

  • bruce October 19, 2004 8:12 PM

    hi,
    thanks for a quick reply, I am searching for how to actually do that in code, and i found it needs some xsl file. I dont have any of those. Can you please guid me with that. Also if its not too hard can you give me the code to do that. Currently i am using the following code:
    FileStream fStream = new FileStream("C:\\TEMP\\test.xml",FileMode.Open,FileAccess.ReadWrite);
    System.Xml.XmlTextReader myXmlTextReader= new System.Xml.XmlTextReader(fStream);
    dataSetXml.ReadXmlSchema(myXmlTextReader);
    fStream.Close();
    dataSetXml.ReadXml("C:\\TEMP\\test.xml", XmlReadMode.IgnoreSchema);

    Thanks for ur help.

  • bruce October 11, 2005 2:51 AM

    Hi ,
    I have similar Problem ,As the Xml file is generate from another program,most probably serilization of object, the xml file cann't be changed. I need to do it programatically.

    I have seen In .net 2.0 ,xmlReader classs has methods like MoveToFollowing and ReadSubtree Method ,that will solve problem but not in .net 1.1

    Cheers
    Rohit

  • Nital January 25, 2008 12:56 AM

    'A column named 'comments' already belongs to this DataTable.'

    I am also getting same problem in FAMOUS rss News.

Leave a Comment

(required) 
(optional)
(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS