C# XmlWriter 写入字符串而不是文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/955611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XmlWriter to Write to a String Instead of to a File
提问by Blaze
I have a WCF service that needs to return a string of XML. But it seems like the writer only wants to build up a file, not a string. I tried:
我有一个需要返回一串 XML 的 WCF 服务。但似乎作者只想建立一个文件,而不是一个字符串。我试过:
string nextXMLstring = "";
using (XmlWriter writer = XmlWriter.Create(nextXMLstring))
This generates an error saying nextXMLstring doesnt have a file path. It wants something like:
这会生成一个错误,指出 nextXMLstring 没有文件路径。它想要这样的东西:
using (XmlWriter writer = XmlWriter.Create("nextXMLstring.xml"))
How can I build up my XML and then return it as a string??
如何构建我的 XML,然后将其作为字符串返回?
Thanks!!
谢谢!!
采纳答案by Richard
You need to create a StringWriter, and pass that to the XmlWriter.
您需要创建一个 StringWriter,并将其传递给 XmlWriter。
The string overload of the XmlWriter.Create is for a filename.
XmlWriter.Create 的字符串重载用于文件名。
E.g.
例如
using (var sw = new StringWriter()) {
using (var xw = XmlWriter.Create(sw)) {
// Build Xml with xw.
}
return sw.ToString();
}
回答by Jon Skeet
As Richard said, StringWriter
is the way forward. There's one snag, however: by default, StringWriter
will advertise itself as being in UTF-16. UsuallyXML is in UTF-8. You can fix this by subclassing StringWriter;
正如理查德所说,StringWriter
是前进的道路。然而,有一个障碍:默认情况下,StringWriter
会将自己宣传为 UTF-16。通常XML 是 UTF-8。你可以通过继承 StringWriter 来解决这个问题;
public class Utf8StringWriter : StringWriter
{
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}
}
This will affect the declaration written by XmlWriter
. Of course, if you then write the string out elsewhere in binary form, make sure you use an encoding which matches whichever encoding you fix for the StringWriter
. (The above code always assumes UTF-8; it's trivial to make a more general version which accepts an encoding in the constructor.)
这将影响由 编写的声明XmlWriter
。当然,如果您随后以二进制形式在别处写出字符串,请确保您使用的编码与您为StringWriter
. (上面的代码总是假设 UTF-8;制作一个更通用的版本来接受构造函数中的编码是微不足道的。)
You'd then use:
然后你会使用:
using (TextWriter writer = new Utf8StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(writer))
{
...
}
return writer.ToString();
}
回答by brianary
I know this is old and answered, but here is another way to do it. Particularly if you don't want the UTF8 BOM at the start of your string and you want the text indented:
我知道这是旧的并已回答,但这是另一种方法。特别是如果您不希望在字符串开头使用 UTF8 BOM 并且希望文本缩进:
using (var ms = new MemoryStream())
using (var x = new XmlTextWriter(ms, new UTF8Encoding(false))
? ?{ Formatting = Formatting.Indented })
{
??// ...
??return Encoding.UTF8.GetString(ms.ToArray());
}
回答by Ria
Use StringBuilder
:
使用StringBuilder
:
var sb = new StringBuilder();
using (XmlWriter xmlWriter = XmlWriter.Create(sb))
{
...
}
return sb.ToString();
回答by saunupe1911
Guys don't forget to call xmlWriter.Close() and xmlWriter.Dispose() or else your string won't finish creating. It will just be an empty string
伙计们不要忘记调用 xmlWriter.Close() 和 xmlWriter.Dispose() 否则您的字符串将无法完成创建。它只是一个空字符串
回答by Deniz
Well I think the simplest and fastest solution here would be just to:
好吧,我认为这里最简单和最快的解决方案就是:
StringBuilder sb = new StringBuilder();
using (var writer = XmlWriter.Create(sb, settings))
{
... // Whatever code you have/need :)
sb = sb.Replace("encoding=\"utf-16\"", "encoding=\"utf-8\""); //Or whatever uft you want/use.
//Before you finally save it:
File.WriteAllText("path\dataName.xml", sb.ToString());
}