Search code examples
asp.netxmlvb.netcdata

Asp.net XMLwriter CData Escape Characters


I use ASP.net VB.net to write a XML file.

In one of the element which is called "Description" i have to add a "<![CData[Class : <b>Class Name</b><br>Price: 100,000.00]]>".

Using,

strDes = "<![CDATA[Class : <b>" + myReader.GetSqlValue(4).ToString + 
    "</b><br>Price: " + myReader.GetSqlValue(7).ToString + "]]>"
XMLwrite.WriteElementString("description", strDes.ToString)

But when i generate the XML file, it gives

<description>&lt;![CDATA[Class : &lt;b&gt;Residential - Site Built&lt;/b&gt;&lt;br&gt;Price: 100,000.00]]&gt;</description>

Solution

  • You should always use the XML APIs to create XML. The CDATA you were generating should have been generated by the XmLWriter API. Try this:

    strDes = "Class : <b>" + myReader.GetSqlValue(4).ToString + _
        "</b><br>Price: " + myReader.GetSqlValue(7).ToString
    XMLwrite.WriteStartElement("description")
    XMLwrite.WriteCData(strDes);
    XMLwrite.WriteEndElement();