How to Prevent the conversion of & to & using XmlTextWriter?
Asked Answered
H

3

6

The '&' in the text gets escaped and gets converted to & when creating the xml file using XmlTextWriter but i dont want the conversion to take place how to prevent it?

Is there any other way besides using WriteRaw func of xmltextwriter?

Hefter answered 1/2, 2010 at 13:13 Comment(2)
What's wrong with using WriteRaw and why don't you want it escaped?Appulse
well i just want extract raw data and represent it as xml.Probably not use it read the xml as it would throw an error.Hefter
H
10

If you put an unescaped ampersand in XML it is no longer valid XML.

Your two choices are either escape it (which your library is doing):

<tag>One &amp; another</tag>

Or wrap it in CDATA:

<tag><![CDATA[One & another]]></tag>

which can be done by:

xmlWriter.WriteCData("One & another");
Hydrate answered 1/2, 2010 at 13:19 Comment(2)
I concur. Having an unescaped & character in XML is a syntax error, and according to the specification at w3.org/TR/xml must be rejected by an XML parser.Contradict
You are right, i just want it in a xml format thats all, i am not going to read itHefter
E
0

Why don't you want this to happen ? It looks to me like the library is enforcing correct character escaping as required by XML.

Escort answered 1/2, 2010 at 13:16 Comment(10)
some people just need it. dont answer a question with another question. tell him how, or say nothing at all.Tedesco
Lol @Chris. Sometimes you learn a lot more when your question is answered with a question, unless you are lazy...Muth
@Rob Maybe, but a question is still not an answer, and is just getting in the way of the actual answers (especially as for some reason its being up-voted) - this should have been a comment.Quartana
@Chris: A counter-question is of course an answer. It makes the other one think. Most if not all questions that are replied to with another question are either not well thought-out (95%) or not clearly formulated (5%). Making questioners think leads to greater understanding for them, since an answer that you find yourself weighs more than one that your have been just told. Besides, not questioning motives is a foolish mindset for a programmer.Retrospective
@Retrospective - thanks for that. That was a more lucid comment than the one I was just formulating.Escort
Counter questions are necessary i suppose as it enables us to interact thereby creating a discussion :PHefter
Well i just wanted to output the data in xml format, i know it would throw an error if u dont escape itHefter
@kragen when you say "not an answer", do you mean "not an answer in the stackoverflow sense"? I agree that maybe this would have been more appropriate as a comment on the question.Muth
I'll tell you why I don't want it to happen. XmlWriter is taking &#8220; and converting it to &amp;#8220;. That's why. Having trouble finding a solution.Cellist
One thing is if the author does not know the pros and cons of having a proper XML. Another thing is if he explicitely does not want to have the values escaped. I do not want this either and I'm also looking for a solution as I do not really care whether the XML is proper or not. I have to be able to read it and reading &amp; or &gt; is harder than & or >. My program handles the parsing properly, but sadly I cannot get it to replace things as I want it.Bivins
M
0

I am not sure if it will work, but you might want to check into the XmlWriterSettings Properties available through xmltextwriter.Settings, especially the CheckCharacters property.

On the other hand, as Brian Agnew mentioned, not encoding the & to & might be the wrong way to go as it will invalidate your XML (unless you already encoded it to & in which case you might just want to decode it before giving it to the xmltextwriter class.

Moya answered 1/2, 2010 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.