转换的XmlDocument为String [英] Convert XmlDocument to String
问题描述
下面是如何我目前正在转换的的XMLDocument 为字符串
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.
如:
<Campaign name="ABC">
</Campaign>
以上是预期的XML。但它返回
Above is the expected XML. But it returns
<Campaign name=\"ABC\">
</Campaign>
我可以做的与string.replace\\,但该方法好吗?是否有副作用吗?它会正常工作,如果XML本身含有的\\
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 "\"
推荐答案
有没有任何引号。这只是VS调试器。尝试打印到控制台或保存到一个文件,你会看到。作为一个方面说明:始终处理一次性对象:
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();
}
这篇关于转换的XmlDocument为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!