本文介绍了的XmlWriter写入一个字符串,而不是到一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有需要返回XML字符串的WCF服务。但它似乎像笔者只希望建立一个文件,而不是一个字符串。我想:
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:
string nextXMLstring = "";
using (XmlWriter writer = XmlWriter.Create(nextXMLstring))
这会产生一个错误,说nextXMLstring不必须的文件路径。它希望是这样的:
This generates an error saying nextXMLstring doesnt have a file path. It wants something like:
using (XmlWriter writer = XmlWriter.Create("nextXMLstring.xml"))
我怎样才能建立我的XML,然后返回一个字符串??
How can I build up my XML and then return it as a string??
谢谢!
推荐答案
您需要创建一个StringWriter的,并传递到XmlWriter的。
You need to create a StringWriter, and pass that to the XmlWriter.
在XmlWriter.Create的字符串超载是一个文件名。
The string overload of the XmlWriter.Create is for a filename.
例如
using (var sw = new StringWriter()) {
using (var xw = XmlWriter.Create(sw)) {
// Build Xml with xw.
}
return sw.ToString();
}
这篇关于的XmlWriter写入一个字符串,而不是到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!