我想创建一个xml文档并将其保存在字符串对象中。
我在SO上提到这两个问题是为了保存和编码目的
我的代码:
StringBuilder builder = new StringBuilder();
XDocument doc = new XDocument(new XElement("BANKID",
new XElement("ReqID", "WEB"),
new XElement("ReqHeader", "CASHDEP"),
new XElement("CurrencyCode", ""),
new XElement("CurrencyAbbrivation", "")));
using (TextWriter writer = new Utf8StringWriter(builder)) // Error Line
{
doc.Save(writer);
}
builder.ToString();
public class Utf8StringWriter : StringWriter
{
public override Encoding Encoding { get { return Encoding.UTF8; } }
}
它给出了错误说" Utf8StringWriter不包含带有1个参数的构造函数"
如何修改接受StringBuilder
参数的类?
答案 0 :(得分:1)
您的自定义Utf8StringWriter
类没有构造函数接受参数,但您尝试将值传递给它。这就是您收到错误消息的原因。
添加一个接受StringBuilder
的公共构造函数,然后将其传递给基类:
public class Utf8StringWriter : StringWriter
{
public Utf8StringWriter(StringBuilder stringBuilder)
:base(stringBuilder)
{
}
public override Encoding Encoding { get { return Encoding.UTF8; } }
}