How to escape text in CSharp console program

  • Thread starter Thread starter Alan Mosley
  • Start date Start date
A

Alan Mosley

I have a console app, that creates a xml file, I need to escape the data
before dumping it in the xml file.

I would use Server.HTMLEncode if it was ASP.NET.

I tried System.Xml.XmlConvert.EncodeName("a&b")

this gives me a_x0026_b I was exspecting a&b

Maybe thios is ok. the xml file is a google sitemap, it need to be read by
google bot, will it read a_x0026_b as a&b I dont know.

any help, ideas?
 
I have a console app, that creates a xml file, I need to escape the data
before dumping it in the xml file.

I would use Server.HTMLEncode if it was ASP.NET.

As an answer to the question "How can I HTMLEncode a string in a
non-ASP application", try adding a reference to System.Web, and use
(e.g.)

string encodedString =
System.Web.HttpUtility.HtmlEncode("a&b");

I tried System.Xml.XmlConvert.EncodeName("a&b")

this gives me a_x0026_b I was exspecting a&b

Maybe thios is ok. the xml file is a google sitemap, it need to be read by
google bot, will it read a_x0026_b as a&b I dont know.

any help, ideas?

A question that comes to mind is why you need to escape the data
yourself though - how is your console application creating the XML
file?

Regards,
Gilles.
 
I have a console app, that creates a xml file, I need to escape the data
before dumping it in the xml file.

How are you creating the file? If you're just explicitly writing each
part of it to disk, why not use all the built-in XML APIs, e.g. build
up an XmlDocument and save that. It's a lot simpler than working out
where to encode what, IMO.

Jon
 
I have a console app, that creates a xml file, I need to escape the
data before dumping it in the xml file.

I would use Server.HTMLEncode if it was ASP.NET.

It sounds like you're using strings to build up your XML. You might want to
consider using one of the XML API's -- that will take care of entity-fying
magic characters, and will probably also give you fewer issues w.r.t.
encodings.

However, if you want to stick to the string-based approach, take a look at
System.Web.HttpUtility.HtmlEncode.
 
Back
Top