Convert XmlDocument to String

Here is how I'm currently converting XMLDocument to String

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);


return stringWriter.ToString();

The problem with this method is that if I have " ((quotes) which I have in attributes) it escapes them.

For Instance:

<Campaign name="ABC">

Above is the expected XML. But it returns

<Campaign name=\"ABC\">

I can do String.Replace "\" but is that method okay? Are there any side-effects? Will it work fine if the XML itself contains a "\"


There aren't any quotes. It's just VS debugger. Try printing to the console or saving to a file and you'll see. As a side note: always dispose disposable objects:

using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
    return stringWriter.GetStringBuilder().ToString();