6

I'm parsing a string of XML into an XDocument that looks like this (using XDocument.Parse)

<Root>
  <Item>Here is &quot;Some text&quot;</Item>
</Root>

Then I manipulate the XML a bit, and I want to send it back out as a string, just like it came in

<Root>
  <Item>Here is &quot;Some text&quot;</Item>
  <NewItem>Another item</NewItem>
</Root>

However, what I am getting is

<Root>
  <Item>Here is \"Some text\"</Item>
  <NewItem>Another item</NewItem>
</Root>

Notice how the double quotes are now escaped instead of encoded?

This happens whether I use

ToString(SaveOptions.DisableFormatting);

or

var stringWriter = new System.IO.StringWriter();
xDoc.Save(stringWriter, SaveOptions.DisableFormatting);
var newXml = stringWriter.GetStringBuilder().ToString();

How can I have the double quotes come out as &quot; and not \"?

UPDATE: Maybe this can explain it better:

var origXml = "<Root><Item>Here is \"Some text&quot;</Item></Root>";
Console.WriteLine(origXml);
var xmlDoc = System.Xml.Linq.XDocument.Parse(origXml);
var modifiedXml = xmlDoc.ToString(System.Xml.Linq.SaveOptions.DisableFormatting);
Console.WriteLine(modifiedXml);

the output I get from this is:

<Root><Item>Here is "Some text&quot;</Item></Root>
<Root><Item>Here is "Some text"</Item></Root>

I want the output to be:

<Root><Item>Here is "Some text&quot;</Item></Root>
<Root><Item>Here is "Some text&quot;</Item></Root>
davekaro
  • 6,233
  • 4
  • 28
  • 22
  • 2
    I don't particularly understand the need for either, actually. `"` is only needed in attribute values, anyway. – Joey Feb 18 '10 at 22:03
  • Well, you are probably right. The third party tool I'm using to read in my XML string was giving an error. The only thing I could see that was different was the way the double quotes looked in the XML string. I was hoping that changing them from \" to " would work - but it doesn't seem to be working. – davekaro Feb 19 '10 at 14:10
  • I've tested in VB.NET based on the above (read in string XML from XDocument.Parse, add a new XElement, write back to a string, save the string) and it's working just fine, just the double quotes and no escapement. Is there something you're doing beyond the above that could help troubleshoot the issue? – Todd Main Feb 20 '10 at 01:28
  • What tool are you using that's giving you a problem? If it can't read XML, I'd like to avoid it. – John Saunders Feb 22 '10 at 14:30
  • Seeing as far as XML is concerned " and " are completely identical in every way as far as the XML Parser is concerned there is no way to control which is written out without doing some manual process of the output string yourself. Perhaps you can explain why you want this strange behaviour? – samjudson Feb 22 '10 at 16:20
  • The more I got into it, the more it seemed this was normal XML and it shouldn't matter. I'm using iBiz QuickBooks components to communicate with QuickBooks. These let you create objects, which it serializes to XML and sends to QuickBooks. The objects can be exported to a XML string. So, I was parsing the string, adding custom XML elements, then re-importing the new XML. But, I kept getting "Checksum error" when trying to import. I narrowed it down to the double quotes. When they were " - it worked fine. If they were " - it failed. I'm doing it differently now, but I guess iBiz is at fault – davekaro Feb 22 '10 at 19:53

2 Answers2

2

You should use an XmlWriter to ensure characters are encoded correctly. However, I'm not sure that you can get the exact output you want without rolling your own class to output the text.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
0

Not tested and don't know if this is solution for you, but try replacing this

var origXml = "<Root><Item>Here is \"Some text&quot;</Item></Root>";

with this

var origXml = "<Root><Item>Here is \"Some text&amp;quot;</Item></Root>";