添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Here is how I'm currently converting XMLDocument to String

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
xmlDoc.WriteTo(xmlTextWriter);
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">
</Campaign>

Above is the expected XML. But it returns

<Campaign name=\"ABC\">
</Campaign>

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 "\"

Assuming xmlDoc is an XmlDocument object whats wrong with xmlDoc.OuterXml?

return xmlDoc.OuterXml;

The OuterXml property returns a string version of the xml.

Well you are right. I tried that first but when I saw the quotes in the debugger. I thought its because of OuterXml and try that StringWriter method. But I was wrong the quotes were only in the debugger. So I'll just use OuterXml. Thanks – akif Mar 9, 2010 at 7:43 Worked for me! Just do a null check on xmlDoc! return xmlDoc == null ? "xmlDoc is null" : xmlDoc.OuterXml; – SarjanWebDev Oct 27, 2014 at 1:04 @SarjanWebDev if you think your object could be null, then the null-check goes without saying and for every answer here; and really for every interaction with an object in C# - personally I try to ensure objects are always constructed so that I don't have to bloat the code with null-checks everywhere... i guess it just depends on your code base... – Chris Moutray Oct 27, 2014 at 6:09 @hazjack if you want declarative tag then make use of the XmlDeclaration Class msdn.microsoft.com/en-us/library/system.xml.xmldeclaration.aspx - if you then want to write it to file then then use the save method found on you XmlDocument instance msdn.microsoft.com/en-us/library/dw229a22.aspx - to me the declarative tag only makes sense if you're writing to say file and not a C# string – Chris Moutray Sep 28, 2015 at 8:52

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))
    xmlDoc.WriteTo(xmlTextWriter);
    xmlTextWriter.Flush();
    return stringWriter.GetStringBuilder().ToString();
                Worthing noting (as mentioned in the remarks here: msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx) that the elements and attributes will not be written until you call the Close method of the XmlWriter.
– TonE
                Aug 23, 2011 at 14:47
                Some of them may be written immediately.  It depends on buffering.  Regardless, I added a call to Flush, though a call to Close or just closing the xmlTextWriter using block would accomplish the same.
– Brian
                Mar 27, 2012 at 16:46
                To make the xml pretty, pass in this settings object to XmlWriter.Create: XmlWriterSettings settings = new XmlWriterSettings();                     settings.Indent = true;                     settings.IndentChars = "    ";                     settings.Encoding = Encoding.UTF8;
– Adam Bruss
                Sep 18, 2012 at 16:44
    public static string AsString(this XmlDocument xmlDoc)
        using (StringWriter sw = new StringWriter())
            using (XmlTextWriter tx = new XmlTextWriter(sw))
                xmlDoc.WriteTo(tx);
                string strXmlText = sw.ToString();
                return strXmlText;

Now to use simply:

yourXmlDoc.AsString()
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.