4 Replies - 3667 Views - Last Post: 03 July 2009 - 05:46 PM Rate Topic: -----

#1 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 178
  • Joined: 26-August 08

Escaping illegal characters and writing to an XML file

Post icon  Posted 03 July 2009 - 03:23 PM

Hi all,

I'm having a bit of trouble writing to an xml file, but I keep getting stuck with illegal characters.

I have been trying to use the escape function (shown below)

string SafeXml = System.Security.SecurityElement.Escape("<?xml version="1.0"?>");


So using this, I'm passing in my string as you can see. However, the compiler gets confused because I'm giving it extra quotes. How do I get around this?
I must have the 1.0 surrounded in quotes in the XML, but I can't find an easy way to do this, can anyone help?

Premier2k

This post has been edited by Premier2k: 03 July 2009 - 03:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Escaping illegal characters and writing to an XML file

#2 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 178
  • Joined: 26-August 08

Re: Escaping illegal characters and writing to an XML file

Posted 03 July 2009 - 03:39 PM

ok, I think I solved the quotes problem, I can escape them using the backslash - here is where I got that information if anyone is interested - here

But now when I check the file it has printed the correct characters for the xml, except that when I try to open the xml it complains there is an error.....I'm confused....What am I doing wrong??

Premier2k
Was This Post Helpful? 0
  • +
  • -

#3 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 178
  • Joined: 26-August 08

Re: Escaping illegal characters and writing to an XML file

Posted 03 July 2009 - 03:47 PM

Getting closer again!

I now have this:

string SafeXml = ("<?xml version=\"1.0\"?>");


but I still get an error when opening the file: XML document must have a top level element. Error processing resource 'file:///C:/testit.xml'.

What am I still doing wrong?

Premier2k
Was This Post Helpful? 0
  • +
  • -

#4 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 178
  • Joined: 26-August 08

Re: Escaping illegal characters and writing to an XML file

Posted 03 July 2009 - 04:02 PM

Figured it out!

Here is what I ended up with:

openfile = File.CreateText(path);	
string SafeXml = "<?xml version=\"1.0\"?>";
string SafeXml2 = "<map>";
string test = "<title>"+filename+"</title>";
string SafeXml3 = @"</map>";


I think I was trying to make this as complicated as I could when it was actually quite simple.

Premier2k
Was This Post Helpful? 0
  • +
  • -

#5 indrora  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 25-July 08

Re: Escaping illegal characters and writing to an XML file

Posted 03 July 2009 - 05:46 PM

the "safe" version of the string "<?xml version="1.0" ?>" is:
"&lt;?xml version=&quot;1.0&quot; ?&gt;"

what you're looking at is "well-formed" xml. "Well-Formed" simply means there's an XML declaration (possibly with Doctype) and a root node with children that close themselves appropriately.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1