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?

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
kurozakura
  • 2,339
  • 8
  • 28
  • 41

3 Answers3

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");
Paolo
  • 22,188
  • 6
  • 42
  • 49
  • I concur. Having an unescaped `&` character in XML is a syntax error, and according to the specification at http://www.w3.org/TR/xml/ **must** be rejected by an XML parser. – lavinio Feb 01 '10 at 13:49
  • You are right, i just want it in a xml format thats all, i am not going to read it – kurozakura Feb 01 '10 at 14:34
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.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 3
    some people just need it. dont answer a question with another question. tell him how, or say nothing at all. – Chris Feb 01 '10 at 13:18
  • 2
    Lol @Chris. Sometimes you learn a lot more when your question is answered with a question, unless you are lazy... – Rob Fonseca-Ensor Feb 01 '10 at 13:22
  • 5
    @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. – Justin Feb 01 '10 at 13:31
  • 1
    @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. – Tomalak Feb 01 '10 at 14:07
  • @Tomalak - thanks for that. That was a more lucid comment than the one I was just formulating. – Brian Agnew Feb 01 '10 at 14:08
  • Counter questions are necessary i suppose as it enables us to interact thereby creating a discussion :P – kurozakura Feb 01 '10 at 14:36
  • Well i just wanted to output the data in xml format, i know it would throw an error if u dont escape it – kurozakura Feb 01 '10 at 14:37
  • 1
    @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. – Rob Fonseca-Ensor Feb 01 '10 at 15:10
  • I'll tell you why I don't want it to happen. XmlWriter is taking “ and converting it to &#8220;. That's why. Having trouble finding a solution. – birdus Apr 10 '13 at 21:23
  • 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 & or > is harder than & or >. My program handles the parsing properly, but sadly I cannot get it to replace things as I want it. – Igor Jun 09 '13 at 03:27
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.