2010-07-20 20 views
8

Các mã sau đây tạo ra sản lượng này:Làm thế nào tôi có thể buộc XDocument xuất ra "UTF-8" trong dòng khai báo?

<?xml version="1.0" encoding="utf-16" standalone="yes"?> 
<customers> 
    <customer> 
    <firstName>Jim</firstName> 
    <lastName>Smith</lastName> 
    </customer> 
</customers> 

Làm thế nào tôi có thể lấy nó để sản xuất encoding="utf-8" thay vì 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); 
     } 
    } 
} 

Trả lời

15

Cho phép tôi trả lời câu hỏi của riêng tôi, điều này dường như làm việc:

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

Dường như một lỗi trong API cho tôi rằng trình tuần tự bỏ qua giá trị này trong 'XDeclaration' của bạn. –

+0

@KirkWoll Không thực sự, vì mã hóa mặc định cho XML là UTF8 nên nó có thể được bỏ qua. Do đó, nó đặt ra câu hỏi tại sao nhu cầu viết UTF-8 rõ ràng ở đó. Có lẽ vấn đề là UTF-16 ở đó, không phải là không có UTF-8. –

+0

giải pháp đơn giản nhất và hoạt động tuyệt vời! – BJladu4

11

Đây không phải là một lỗi trong .NET. Điều này là do bạn sử dụng StringWriter làm mục tiêu cho XDocument của mình. Vì StringWriter sử dụng nội bộ UTF-16, tài liệu cũng phải sử dụng UTF-16 làm mã hóa. Nếu bạn lưu XDoc vào luồng hoặc tệp, nó sẽ sử dụng UTF-8 theo hướng dẫn.

Để biết thêm thông tin, xem MSDN information about StringWriter.Encoding:

Khách sạn này là cần thiết đối với một số kịch bản XML nơi một tiêu đề phải được viết có chứa các mã hóa được sử dụng bởi StringWriter. Điều này cho phép mã XML tiêu thụ một StringWriter tùy ý và tạo ra tiêu đề XML đúng.

Các vấn đề liên quan