Skip to content Skip to sidebar Skip to footer

Asp.net Special Character Problem

I'm building an automated RSS feed in ASP.NET and occurrences of apostrophes and hyphens are rendering very strangely: 'Here's a test' is rendering as 'Here’s a test' I ha

Solution 1:

I would guess the the column in your SQL 2005 database is defined as a varchar(N), char(N) or text. If so the conversion is due to the database driver using a different code page setting to that set in the database.

I would recommend changing this column (any any others that may contain non-ASCII data) to nvarchar(N), nchar(N) or nvarchar(max) respectively, which can then contain any Unicode code point, not just those defined by the code page.

All of my databases now use nvarchar/nchar exclusively to avoid these type of encoding issues. The Unicode fields use twice as much storage space but there'll be very little performance difference if you use this technique (the SQL engine uses Unicode internally).

Solution 2:

Transpires that the data (whilst showing in SQLServer plain) is actually carrying some MS Word special characters.

Solution 3:

Assuming you get Unicode-characters from the database, the easiest way is to let System.Xml.dll take care of the conversion for you by appending the RSS-feed with a XmlDocument object. (I'm not sure about the elements found in a rss-feed.)

        XmlDocument rss = new XmlDocument();
        rss.LoadXml("<?xml version='1.0'?><rss />");
        XmlElement element = rss.DocumentElement.AppendChild(rss.CreateElement("item")) as XmlElement;
        element.InnerText = sArticleSummary;

or with Linq.Xml:

XDocumentrss=newXDocument(
            newXElement("rss",
                newXElement("item", sArticleSummary)
            )
        );

Solution 4:

I would just put "Here's a test" into a CDATA tag. Easy and it works.

<![CDATA[Here's a test]]>

Post a Comment for "Asp.net Special Character Problem"