XmlWriterSettings.Encoding XmlWriterSettings.Encoding XmlWriterSettings.Encoding XmlWriterSettings.Encoding Property

定義

取得或設定要使用的文字編碼方式類型。Gets or sets the type of text encoding to use.

public:
 property System::Text::Encoding ^ Encoding { System::Text::Encoding ^ get(); void set(System::Text::Encoding ^ value); };
public System.Text.Encoding Encoding { get; set; }
member this.Encoding : System.Text.Encoding with get, set
Public Property Encoding As Encoding

屬性值

要使用的文字編碼方式。The text encoding to use. 預設為 Encoding.UTF8The default is Encoding.UTF8.

範例

下列範例會將 XML 片段寫入記憶體資料流。The following example writes an XML fragment to a memory stream.

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;

// Create the XmlWriter object and write some content.
MemoryStream strm = new MemoryStream();
XmlWriter writer = XmlWriter.Create(strm, settings);
writer.WriteElementString("orderID", "1-456-ab");
writer.WriteElementString("orderID", "2-36-00a");
writer.Flush();
writer.Close();

// Do additonal processing on the stream.
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.OmitXmlDeclaration = true
settings.ConformanceLevel = ConformanceLevel.Fragment
settings.CloseOutput = false

' Create the XmlWriter object and write some content.
Dim strm as MemoryStream = new MemoryStream()
Dim writer As XmlWriter = XmlWriter.Create(strm, settings)
writer.WriteElementString("orderID", "1-456-ab")
writer.WriteElementString("orderID", "2-36-00a")
writer.Flush()
writer.Close()

' Do additonal processing on the stream.

備註

XmlWriter次,而不是字元的字元編碼的字元緩衝區。The XmlWriter encodes a buffer of characters all at once, rather than character by character. 例外狀況時擲回Flush如果發生任何編碼的錯誤,則會呼叫方法。An exception is thrown when the Flush method is called if any encoding errors are encountered.

Encoding屬性只適用於XmlWriter皆可建立具有指定的執行個體Stream或使用指定的檔案名稱。The Encoding property only applies to the XmlWriter instances that are created either with the specified Stream or with the specified file name. 如果XmlWriter會建立執行個體具有指定之TextWriter,則Encoding屬性由基礎的編碼方式覆寫TextWriterIf the XmlWriter instance is created with the specified TextWriter, the Encoding property is overridden by the encoding of the underlying TextWriter. 例如,如果這個屬性設定為 Unicode (utf-16) 特定XmlWriter,但基礎寫入器StreamWriter(衍生自TextWriter) 使用其編碼設定為 UTF8,則輸出為 utf-8 編碼。For example, if this property is set to Unicode (UTF-16) for a particular XmlWriter, but the underlying writer is a StreamWriter (which derives from TextWriter) with its encoding set to UTF8, the output will be UTF-8 encoded.

如果XmlWriter與其他輸出參數,建立執行個體Encoding屬性會被忽略。If the XmlWriter instance is created with other output parameters, the Encoding property is ignored.

在以下範例中:In the following example:

using (StreamWriter output =  
    new StreamWriter(new FileStream("Xml01.xml", FileMode.Create), Encoding.Unicode))  
{  
    using (XmlWriter xmlWriter =  
        XmlWriter.Create(output, new XmlWriterSettings()))  
    {  
        xmlWriter.WriteStartDocument();  
        xmlWriter.WriteStartElement("Root");  
        xmlWriter.WriteEndElement();  
        xmlWriter.WriteEndDocument();  
    }  
}  

重要

不接受Encoding來自不受信任的來源物件。Do not accept an Encoding object from an untrusted source.

適用於