XmlWriterSettings.Encoding Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
Property Value
The text encoding to use. The default is Encoding.UTF8
.
Examples
The following example shows how to set the Encoding
property:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = Encoding.Unicode;
XmlWriter writer = XmlWriter.Create("books.xml", settings);
Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.Encoding = Encoding.Unicode
Dim writer As XmlWriter = XmlWriter.Create("books.xml", settings)
Remarks
The XmlWriter encodes a buffer of characters all at once, rather than character by character. An exception is thrown when the Flush method is called if any encoding errors are encountered.
The Encoding property only applies to the XmlWriter instances that are created either with the specified Stream or with the specified file name. If the XmlWriter instance is created with the specified TextWriter, the Encoding property is overridden by the encoding of the underlying TextWriter. 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.
If the XmlWriter instance is created with other output parameters, the Encoding property is ignored.
In the following example:
The default value of Encoding is
Encoding.UTF8
.The StreamWriter is set to
Encoding.Unicode
and the StreamWriter's encoding overrides the Encoding setting.The output will be
Encoding.Unicode
.
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();
}
}
Important
Do not accept an Encoding object from an untrusted source.
Applies to
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기