471,305 Members | 1,296 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

How to escape text in CSharp console program

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?
Jun 27 '08 #1
3 1703
On Thu, 22 May 2008 15:44:03 +0800, "Alan Mosley" <me@workwrote:
>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&amp;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.

Jun 27 '08 #2
On May 22, 8:44 am, "Alan Mosley" <me@workwrote:
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
Jun 27 '08 #3
"Alan Mosley" <me@workwrote in
news:#x**************@TK2MSFTNGP02.phx.gbl:
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.
--
Arnout.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ken | last post: by
reply views Thread by Daniel Perron | last post: by
16 posts views Thread by Christine | last post: by
12 posts views Thread by Adam J. Schaff | last post: by
1 post views Thread by not_a_commie | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.