XDeclaration.Encoding 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置此文档的编码。
public:
property System::String ^ Encoding { System::String ^ get(); void set(System::String ^ value); };
public string Encoding { get; set; }
public string? Encoding { get; set; }
member this.Encoding : string with get, set
Public Property Encoding As String
属性值
一个包含此文档的代码页名称的 String。
示例
下面的示例创建两个文档,一个文档使用 utf-8 编码,另一个使用 utf-16 编码。 然后,该示例加载这两个文档并将编码输出到控制台。
Console.WriteLine("Creating a document with utf-8 encoding");
XDocument encodedDoc8 = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Root", "Content")
);
encodedDoc8.Save("EncodedUtf8.xml");
Console.WriteLine("Encoding is:{0}", encodedDoc8.Declaration.Encoding);
Console.WriteLine();
Console.WriteLine("Creating a document with utf-16 encoding");
XDocument encodedDoc16 = new XDocument(
new XDeclaration("1.0", "utf-16", "yes"),
new XElement("Root", "Content")
);
encodedDoc16.Save("EncodedUtf16.xml");
Console.WriteLine("Encoding is:{0}", encodedDoc16.Declaration.Encoding);
Console.WriteLine();
XDocument newDoc8 = XDocument.Load("EncodedUtf8.xml");
Console.WriteLine("Encoded document:");
Console.WriteLine(File.ReadAllText("EncodedUtf8.xml"));
Console.WriteLine();
Console.WriteLine("Encoding of loaded document is:{0}", newDoc8.Declaration.Encoding);
Console.WriteLine();
XDocument newDoc16 = XDocument.Load("EncodedUtf16.xml");
Console.WriteLine("Encoded document:");
Console.WriteLine(File.ReadAllText("EncodedUtf16.xml"));
Console.WriteLine();
Console.WriteLine("Encoding of loaded document is:{0}", newDoc16.Declaration.Encoding);
Console.WriteLine("Creating a document with utf-8 encoding")
Dim encodedDoc8 As XDocument = _
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>Content</Root>
encodedDoc8.Save("EncodedUtf8.xml")
Console.WriteLine("Encoding is:{0}", encodedDoc8.Declaration.Encoding)
Console.WriteLine()
Console.WriteLine("Creating a document with utf-16 encoding")
Dim encodedDoc16 As XDocument = _
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<Root>Content</Root>
encodedDoc16.Save("EncodedUtf16.xml")
Console.WriteLine("Encoding is:{0}", encodedDoc16.Declaration.Encoding)
Console.WriteLine()
Dim newDoc8 As XDocument = XDocument.Load("EncodedUtf8.xml")
Console.WriteLine("Encoded document:")
Console.WriteLine(File.ReadAllText("EncodedUtf8.xml"))
Console.WriteLine()
Console.WriteLine("Encoding of loaded document is:{0}", newDoc8.Declaration.Encoding)
Console.WriteLine()
Dim newDoc16 As XDocument = XDocument.Load("EncodedUtf16.xml")
Console.WriteLine("Encoded document:")
Console.WriteLine(File.ReadAllText("EncodedUtf16.xml"))
Console.WriteLine()
Console.WriteLine("Encoding of loaded document is:{0}", newDoc16.Declaration.Encoding)
该示例产生下面的输出:
Creating a document with utf-8 encoding
Encoding is:utf-8
Creating a document with utf-16 encoding
Encoding is:utf-16
Encoded document:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>Content</Root>
Encoding of loaded document is:utf-8
Encoded document:
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<Root>Content</Root>
Encoding of loaded document is:utf-16
注解
由 WebName 返回的任何值都是有效值。
如果读取编码的文档,则将此属性设置为代码页名称。
如果将此属性设置为有效的代码页名称,则在序列化时,LINQ to XML将使用指定的编码进行序列化。