Search code examples
c#xmlvb.netescapingxmltextwriter

How to Prevent the conversion of & to & using XmlTextWriter?


The '&' in the text gets escaped and gets converted to & when creating the xml file using XmlTextWriter but i dont want the conversion to take place how to prevent it?

Is there any other way besides using WriteRaw func of xmltextwriter?


Solution

  • If you put an unescaped ampersand in XML it is no longer valid XML.

    Your two choices are either escape it (which your library is doing):

    <tag>One &amp; another</tag>
    

    Or wrap it in CDATA:

    <tag><![CDATA[One & another]]></tag>
    

    which can be done by:

    xmlWriter.WriteCData("One & another");