Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将xml文档转换为编码为utf-8的字符串_C#_Xml_Encoding_Utf 8 - Fatal编程技术网

C# 将xml文档转换为编码为utf-8的字符串

C# 将xml文档转换为编码为utf-8的字符串,c#,xml,encoding,utf-8,C#,Xml,Encoding,Utf 8,我想创建一个xml文档并将其保存在string对象中 为了保存和编码的目的,我将这两个问题都提到这里 我的代码: StringBuilder builder = new StringBuilder(); XDocument doc = new XDocument(new XElement("BANKID", new XElement("ReqID", "WEB"), new XElement("ReqHeader", "CASHDEP"), n

我想创建一个xml文档并将其保存在string对象中

为了保存和编码的目的,我将这两个问题都提到这里

我的代码:

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
的类?

您的自定义
Utf8StringWriter
类没有接受参数的构造函数,但您正在尝试向其传递值。这就是为什么您会收到错误消息

添加接受
StringBuilder
的公共构造函数,然后将其传递给基类:

public class Utf8StringWriter : StringWriter
{
    public Utf8StringWriter(StringBuilder stringBuilder)
        :base(stringBuilder)
    {
    }

    public override Encoding Encoding { get { return Encoding.UTF8; } }
}
类似于:?