如何强制 XDocument 输出“UTF-8”在声明行?
以下代码产生此输出:
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<customers>
<customer>
<firstName>Jim</firstName>
<lastName>Smith</lastName>
</customer>
</customers>
如何让它产生 encoding="utf-8"
而不是 encoding="utf-16"
?
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
namespace test_xml2
{
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer> {
new Customer {FirstName="Jim", LastName="Smith", Age=27},
new Customer {FirstName="Hank", LastName="Moore", Age=28},
new Customer {FirstName="Jay", LastName="Smythe", Age=44},
new Customer {FirstName="Angie", LastName="Thompson", Age=25},
new Customer {FirstName="Sarah", LastName="Conners", Age=66}
};
Console.WriteLine(BuildXmlWithLINQ(customers));
Console.ReadLine();
}
private static string BuildXmlWithLINQ(List<Customer> customers)
{
XDocument xdoc =
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("customers",
new XElement("customer",
new XElement("firstName", "Jim"),
new XElement("lastName", "Smith")
)
)
);
var wr = new StringWriter();
xdoc.Save(wr);
return wr.GetStringBuilder().ToString();
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Display()
{
return String.Format("{0}, {1} ({2})", LastName, FirstName, Age);
}
}
}
The following code produces this output:
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<customers>
<customer>
<firstName>Jim</firstName>
<lastName>Smith</lastName>
</customer>
</customers>
How can I get it to produce encoding="utf-8"
instead of encoding="utf-16"
?
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
namespace test_xml2
{
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer> {
new Customer {FirstName="Jim", LastName="Smith", Age=27},
new Customer {FirstName="Hank", LastName="Moore", Age=28},
new Customer {FirstName="Jay", LastName="Smythe", Age=44},
new Customer {FirstName="Angie", LastName="Thompson", Age=25},
new Customer {FirstName="Sarah", LastName="Conners", Age=66}
};
Console.WriteLine(BuildXmlWithLINQ(customers));
Console.ReadLine();
}
private static string BuildXmlWithLINQ(List<Customer> customers)
{
XDocument xdoc =
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("customers",
new XElement("customer",
new XElement("firstName", "Jim"),
new XElement("lastName", "Smith")
)
)
);
var wr = new StringWriter();
xdoc.Save(wr);
return wr.GetStringBuilder().ToString();
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Display()
{
return String.Format("{0}, {1} ({2})", LastName, FirstName, Age);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用以下代码作为示例
You can use the following code as an example
这不是 .NET 中的错误。这是因为您使用
StringWriter
作为 XDocument 的目标。由于StringWriter内部使用UTF-16,因此文档也必须使用UTF-16作为编码。如果您将 XDoc 保存到流或文件中,它将按照指示使用 UTF-8。有关详细信息,请参阅 关于
StringWriter.Encoding
的MSDN信息:This is not a bug in .NET. This is due to you using
StringWriter
as the target for your XDocument. Since StringWriter internally uses UTF-16, the document must also use UTF-16 as encoding. If you save the XDoc to a stream or a file, it will use UTF-8 as instructed.For more information, see MSDN information about
StringWriter.Encoding
:让我回答我自己的问题,这似乎有效:
Allow me answer my own question, this seems to work: