Saving XML replaces ' to '

I'm creating an XML file this way:

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

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?

Thanks!
LVL 2
the_7th_kingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
I tried your code, there's not problem with this part.
0
the_7th_kingAuthor Commented:
but it does not save ' as apos;
0
sybeCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

the_7th_kingAuthor Commented:
xFile.InnerXml changes the ' to ' when saving it !
0
sybeCommented:
> 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>"
		oXml.LoadXml(sXml)
		Dim oNode As XmlElement = oXml.CreateElement("element")
		Dim oTextNode As XmlText = oXml.CreateTextNode("&apos;")
		oNode.AppendChild(oTextNode)
		oXml.DocumentElement.AppendChild(oNode)
		Response.ContentType = "text/xml"
		oXml.Save(Response.OutputStream)
		Response.End()
	End Sub

Open in new window

0
MikeTooleCommented:
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; ");

0
MlandaTCommented:
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.
0
the_7th_kingAuthor Commented:
thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.