RSS, ReadXml() and Namespaces
A recent post in a mailing group I am on said they were having problems with their RSS feed and binding it to a datagrid. the problem was that there was two elements, under different namespaces, but which had the same name. Apparently there is a problem (let me know if i'm missing something!) with ReadXml() in that it will only recognize a single namespace.
When trying to bind he was getting the message :
An unhandled exception of type 'System.Data.DuplicateNameException' occurred in system.data.dll
Additional information: A column named 'comments' already belongs to this DataTable.
After a little searching i discovered that quite a few people have posted on this problem (won't say bug in case i'm missing something). So I decided to fire in a little fix that although isn't perfect, it can be reused until the "issue" is understood. So the Xslt I created is below:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://purl.org/dc/elements/1.1/" > <xsl:template match="*|@*|comment()|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|comment()|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="dc:comments" priority="1">
<DCComments><xsl:apply-templates select="*|@*|comment()|text()"/></DCComments>
</xsl:template>
</xsl:stylesheet>
You can modify your original C# code as follows:
System.Xml.Xsl.XslTransform xslt = new System.Xml.Xsl.XslTransform(); xslt.Load("../../trans.xslt");
System.Xml.XPath.XPathDocument xd = new System.Xml.XPath.XPathDocument("../../rss.xml");
System.IO.Stream strmTemp = new System.IO.FileStream("out.xml", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite); xslt.Transform(xd, null, strmTemp, null); strmTemp.Close();
DataSet ds = new DataSet();
ds.ReadXml("../../out.xml");
dataGrid1.DataSource = ds;
Of course you can modify the Xslt to catch whatever elements you like. Not perfect and glad to hear any other suggestions, but it works :)
vtgo.net