Xml writing quotes etc

  • Thread starter Thread starter Sagaert Johan
  • Start date Start date
S

Sagaert Johan

Hi

Is there some kind of method to convert strings containing " < > etc to
some form that it is allowed inside XML tags ?

Johan
 
Sagaert said:
Is there some kind of method to convert strings containing " < > etc to
some form that it is allowed inside XML tags ?

Use XmlWriter to create your XML, it does the necessary escaping for
you. If you only want to create a text then use XmlWriterSettings with
ConformanceLevel.Fragment e.g.

XmlWriterSettings settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
StringWriter writer = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(writer, settings))
{
xmlWriter.WriteString("a < b && b < a");
}
Console.WriteLine(writer.ToString());

writes "a &lt; b &amp;&amp; b &lt; a".
 
Sagaert Johan said:
Is there some kind of method to convert strings containing " < > etc to
some form that it is allowed inside XML tags ?

The best approach is to use an API which lets you build up the XML
document just by specifying the strings, and it will do the escaping
for you - XmlWriter, XmlDocument etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top