• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1483
  • Last Modified:

Saving XML replaces ' to '

I'm creating an XML file this way:

                    XmlDocument xFile = new XmlDocument();                    
                    xFile.InnerXml = XMLStringBuilder.ToString();

The stringbuilder which is used in the InnerXml step has certain character replacements dones:
& to &
' to '
" to "

Now, the problem is, when the xml document gets saved, the ' is written saved as ' and even " is saved as ". Only, & stays as it is.

I don't know why the replaced characters are turning back to their originals... this is totally not desired.

Why is this happening and how do I counter this?

1 Solution
I tried your code, there's not problem with this part.
the_7th_kingAuthor Commented:
but it does not save ' as apos;
It is happening because you are mixing string with xmldocument. Why are you using an XmlDocument if you are using an xml-formatted String?

Solutions: stick to using Strings (and don't use XmlDocument) or use XmlDocument and don't use String. For example: just save the string from your XMLStringBuilder directly to a textfile. Or if you want want to use XmlDocument, then build the XmlDocument by creating nodes and appending those to the xmldocument.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

the_7th_kingAuthor Commented:
xFile.InnerXml changes the ' to ' when saving it !
> xFile.InnerXml changes the ' to ' when saving it !

Yes it does. It should. See attached code to see how it works.

As I said before, you can not just create an xml-formatted string and expect it to be xml.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Dim oXml As New XmlDocument
		Dim sXml As String = "<root><element>&apos;</element></root>"
		Dim oNode As XmlElement = oXml.CreateElement("element")
		Dim oTextNode As XmlText = oXml.CreateTextNode("&apos;")
		Response.ContentType = "text/xml"
	End Sub

Open in new window

Another alternative is to use the alternative support for building XML from Sytem.Xml.Linq
The following assignment doesn't convert the &apos

XElement MyElement = New XElement("Name", "&apos; Mike &apos; ");

I've often found that using an XmlWriter and it's WriteElementString simplify creation of XML documents. The escaping will be handled automatically for you. http://msdn.microsoft.com/en-us/library/aex0e7zs.aspx

As SYBE says, I also think its probably better to decide whether to use your own XML formatted string (in which case, write it directly to the file), or one of the .NET XML manipulation classes (in which case, e.g. the XmlWriter will take care of escaping and formatting for you), but not both.
the_7th_kingAuthor Commented:
thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now