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

Edw*_*uay 9 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>
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Edw*_*uay 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();
}
Run Code Online (Sandbox Code Playgroud)


Sam*_*nen 16

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

有关更多信息,请参阅有关的MSDN信息StringWriter.Encoding:

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