如何强制XDocument在声明行中输出“UTF-8”?

时间:2010-07-20 08:44:27

标签: c# xml utf-8 linq-to-xml

以下代码生成此输出:

<?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);
        }
    }
}

3 个答案:

答案 0 :(得分:16)

请允许我回答我自己的问题,这似乎有效:

private static string BuildXmlWithLINQ()
{
    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")
            )
        )
    );
    return xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString();
}

答案 1 :(得分:13)

这不是.NET中的错误。这是因为您使用StringWriter作为XDocument的目标。由于StringWriter内部使用UTF-16,因此文档还必须使用UTF-16作为编码。如果将XDoc保存到流或文件中,它将按照说明使用UTF-8。

有关详细信息,请参阅MSDN information about StringWriter.Encoding

  

此属性对于某些XML方案是必需的,其中必须编写包含StringWriter使用的编码的标头。这允许XML代码使用任意StringWriter并生成正确的XML头。

答案 2 :(得分:0)

您可以使用以下代码作为示例

XDocument doc = GetXmlDoc();
using (var stream = new MemoryStream())
{
    doc.Save(stream, SaveOptions.DisableFormatting);
    var docBytes = stream.ToArray();
    File.WriteAllBytes("fileName.xml", docBytes);
}