XmlWriter.Create 方法

定義

建立新的 XmlWriter 執行個體。Creates a new XmlWriter instance.

多載

Create(StringBuilder, XmlWriterSettings)

使用 XmlWriterStringBuilder 物件,建立新的 XmlWriterSettings 執行個體。Creates a new XmlWriter instance using the StringBuilder and XmlWriterSettings objects.

Create(String, XmlWriterSettings)

使用檔名和 XmlWriter 物件,建立新 XmlWriterSettings 執行個體。Creates a new XmlWriter instance using the filename and XmlWriterSettings object.

Create(TextWriter, XmlWriterSettings)

使用 XmlWriterTextWriter 物件,建立新的 XmlWriterSettings 執行個體。Creates a new XmlWriter instance using the TextWriter and XmlWriterSettings objects.

Create(Stream, XmlWriterSettings)

使用資料流和 XmlWriter 物件,建立新 XmlWriterSettings 執行個體。Creates a new XmlWriter instance using the stream and XmlWriterSettings object.

Create(XmlWriter, XmlWriterSettings)

使用指定的 XmlWriterXmlWriter 物件,建立新的 XmlWriterSettings 執行個體。Creates a new XmlWriter instance using the specified XmlWriter and XmlWriterSettings objects.

Create(StringBuilder)

使用指定的 XmlWriter,建立新 StringBuilder 執行個體。Creates a new XmlWriter instance using the specified StringBuilder.

Create(String)

使用指定的檔名,建立新 XmlWriter 執行個體。Creates a new XmlWriter instance using the specified filename.

Create(TextWriter)

使用指定的 XmlWriter,建立新 TextWriter 執行個體。Creates a new XmlWriter instance using the specified TextWriter.

Create(Stream)

使用指定的資料流,建立新的 XmlWriter 執行個體。Creates a new XmlWriter instance using the specified stream.

Create(XmlWriter)

使用指定的 XmlWriter 物件,建立新 XmlWriter 執行個體。Creates a new XmlWriter instance using the specified XmlWriter object.

備註

某些多載 Create 包含 settings 接受物件的參數 XmlWriterSettingsSome of the Create overloads include a settings parameter that accepts an XmlWriterSettings object. 您可以使用此物件來:You can use this object to:

  • 指定您想要在所建立物件上支援的功能 XmlWriterSpecify which features you want supported on the created XmlWriter object.

  • 重複使用 XmlWriterSettings 物件來建立多個寫入器物件。Reuse the XmlWriterSettings object to create multiple writer objects. 複製每個已建立之寫入器的 XmlWriterSettings 物件,並標示為唯讀。The XmlWriterSettings object is copied and marked read-only for each created writer. XmlWriterSettings 執行個體上設定的變更不會影響具有相同設定的現有寫入器。Changes to the settings on an XmlWriterSettings instance do not affect existing writers with the same settings. 因此,您可以使用相同設定來建立具有相同功能的多個寫入器。Thus, you can use the same settings to create multiple writers with the same functionality. 或者,您可以修改 XmlWriterSettings 執行個體上的設定,並建立具有一組不同功能的新寫入器。Or, you can modify the settings on an XmlWriterSettings instance and create a new writer with a different set of features.

  • 將功能加入至現有的 XML 寫入器。Add features to an existing XML writer. Create 方法可以接受其他 XmlWriter 物件。The Create method can accept another XmlWriter object. 基礎 XmlWriter 物件不需要是靜態方法所建立的 XML 寫入器 CreateThe underlying XmlWriter object does not have to be an XML writer created by the static Create method. 例如,您可以指定使用者定義的 XML 寫入器,將其他功能加入至。For example, you can specify a user-defined XML writer to add additional features to.

  • 充分利用只有靜態方法所建立之物件所提供之 XML 1.0 建議 的功能,例如更佳的一致性檢查和合規性 XmlWriter CreateTake full advantage of features such as better conformance checking and compliance to the XML 1.0 recommendation that are available only on XmlWriter objects created by the static Create method.

如果您使用 Create 不接受物件的多載 XmlWriterSettings ,則會使用下列預設寫入器設定:If you use a Create overload that doesn't accept an XmlWriterSettings object, the following default writer settings are used:

設定Setting 預設Default
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格Two spaces
NamespaceHandling Default 不 (移除) Default (no removal)
NewLineChars \r\n (歸位字元,新行)\r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

注意

雖然 .NET Framework 包含 XmlTextWriter 類別,這是類別的具體實作為,但 XmlWriter 我們建議您使用方法來建立 XmlWriter 實例 CreateAlthough the .NET Framework includes the XmlTextWriter class, which is a concrete implementation of the XmlWriter class, we recommend that you create XmlWriter instances by using the Create method.

Create(StringBuilder, XmlWriterSettings)

使用 XmlWriterStringBuilder 物件,建立新的 XmlWriterSettings 執行個體。Creates a new XmlWriter instance using the StringBuilder and XmlWriterSettings objects.

public:
 static System::Xml::XmlWriter ^ Create(System::Text::StringBuilder ^ output, System::Xml::XmlWriterSettings ^ settings);
public static System.Xml.XmlWriter Create (System.Text.StringBuilder output, System.Xml.XmlWriterSettings settings);
public static System.Xml.XmlWriter Create (System.Text.StringBuilder output, System.Xml.XmlWriterSettings? settings);
static member Create : System.Text.StringBuilder * System.Xml.XmlWriterSettings -> System.Xml.XmlWriter
Public Shared Function Create (output As StringBuilder, settings As XmlWriterSettings) As XmlWriter

參數

output
StringBuilder

要寫入至其中的 StringBuilderThe StringBuilder to which to write to. XmlWriter 寫入的內容會附加至 StringBuilderContent written by the XmlWriter is appended to the StringBuilder.

settings
XmlWriterSettings

用於設定新 XmlWriterSettings 執行個體的 XmlWriter 物件。The XmlWriterSettings object used to configure the new XmlWriter instance. 如果是 null,則會使用有預設值的 XmlWriterSettingsIf this is null, a XmlWriterSettings with default settings is used.

如果 XmlWriter 正配合 Transform(String, XmlWriter) 方法使用,您應該使用 OutputSettings 屬性,以取得有正確設定的 XmlWriterSettings 物件。If the XmlWriter is being used with the Transform(String, XmlWriter) method, you should use the OutputSettings property to obtain an XmlWriterSettings object with the correct settings. 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。This ensures that the created XmlWriter object has the correct output settings.

傳回

XmlWriter

XmlWriter 物件。An XmlWriter object.

例外狀況

builder 值為 nullThe builder value is null.

適用於

Create(String, XmlWriterSettings)

使用檔名和 XmlWriter 物件,建立新 XmlWriterSettings 執行個體。Creates a new XmlWriter instance using the filename and XmlWriterSettings object.

public:
 static System::Xml::XmlWriter ^ Create(System::String ^ outputFileName, System::Xml::XmlWriterSettings ^ settings);
public static System.Xml.XmlWriter Create (string outputFileName, System.Xml.XmlWriterSettings? settings);
public static System.Xml.XmlWriter Create (string outputFileName, System.Xml.XmlWriterSettings settings);
static member Create : string * System.Xml.XmlWriterSettings -> System.Xml.XmlWriter
Public Shared Function Create (outputFileName As String, settings As XmlWriterSettings) As XmlWriter

參數

outputFileName
String

要寫入其中的檔案。The file to which you want to write. XmlWriter 會於指定路徑建立檔案,並在 XML 1.0 文字語法中寫入它。The XmlWriter creates a file at the specified path and writes to it in XML 1.0 text syntax. outputFileName 必須是檔案系統路徑。The outputFileName must be a file system path.

settings
XmlWriterSettings

用於設定新 XmlWriterSettings 執行個體的 XmlWriter 物件。The XmlWriterSettings object used to configure the new XmlWriter instance. 如果是 null,則會使用有預設值的 XmlWriterSettingsIf this is null, a XmlWriterSettings with default settings is used.

如果 XmlWriter 正配合 Transform(String, XmlWriter) 方法使用,您應該使用 OutputSettings 屬性,以取得有正確設定的 XmlWriterSettings 物件。If the XmlWriter is being used with the Transform(String, XmlWriter) method, you should use the OutputSettings property to obtain an XmlWriterSettings object with the correct settings. 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。This ensures that the created XmlWriter object has the correct output settings.

傳回

XmlWriter

XmlWriter 物件。An XmlWriter object.

例外狀況

url 值為 nullThe url value is null.

範例

下列範例會 XmlWriter 使用已定義的設定來建立物件。The following example creates an XmlWriter object with the defined settings.

using System;
using System.IO;
using System.Xml;
using System.Text;

public class Sample {

  public static void Main() {

    XmlWriter writer = null;

    try {

       // Create an XmlWriterSettings object with the correct options.
       XmlWriterSettings settings = new XmlWriterSettings();
       settings.Indent = true;
       settings.IndentChars = ("\t");
       settings.OmitXmlDeclaration = true;

       // Create the XmlWriter object and write some content.
       writer = XmlWriter.Create("data.xml", settings);
       writer.WriteStartElement("book");
       writer.WriteElementString("item", "tesing");
       writer.WriteEndElement();
    
       writer.Flush();
     }
     finally  {
        if (writer != null)
          writer.Close();
     }
  }
}
Imports System.IO
Imports System.Xml
Imports System.Text

Public Class Sample 

  Public Shared Sub Main() 
  
    Dim writer As XmlWriter = Nothing

    Try 

       ' Create an XmlWriterSettings object with the correct options. 
       Dim settings As XmlWriterSettings = New XmlWriterSettings()
       settings.Indent = true
       settings.IndentChars = (ControlChars.Tab)
       settings.OmitXmlDeclaration = true

       ' Create the XmlWriter object and write some content.
       writer = XmlWriter.Create("data.xml", settings)
       writer.WriteStartElement("book")
       writer.WriteElementString("item", "tesing")
       writer.WriteEndElement()
    
       writer.Flush()

      Finally
         If Not (writer Is Nothing) Then
            writer.Close()
         End If
      End Try

   End Sub 
End Class 

適用於

Create(TextWriter, XmlWriterSettings)

使用 XmlWriterTextWriter 物件,建立新的 XmlWriterSettings 執行個體。Creates a new XmlWriter instance using the TextWriter and XmlWriterSettings objects.

public:
 static System::Xml::XmlWriter ^ Create(System::IO::TextWriter ^ output, System::Xml::XmlWriterSettings ^ settings);
public static System.Xml.XmlWriter Create (System.IO.TextWriter output, System.Xml.XmlWriterSettings settings);
public static System.Xml.XmlWriter Create (System.IO.TextWriter output, System.Xml.XmlWriterSettings? settings);
static member Create : System.IO.TextWriter * System.Xml.XmlWriterSettings -> System.Xml.XmlWriter
Public Shared Function Create (output As TextWriter, settings As XmlWriterSettings) As XmlWriter

參數

output
TextWriter

要寫入至其中的 TextWriterThe TextWriter to which you want to write. XmlWriter 會寫入 XML 1.0 文字語法,並將其附加至指定的 TextWriterThe XmlWriter writes XML 1.0 text syntax and appends it to the specified TextWriter.

settings
XmlWriterSettings

用於設定新 XmlWriterSettings 執行個體的 XmlWriter 物件。The XmlWriterSettings object used to configure the new XmlWriter instance. 如果是 null,則會使用有預設值的 XmlWriterSettingsIf this is null, a XmlWriterSettings with default settings is used.

如果 XmlWriter 正配合 Transform(String, XmlWriter) 方法使用,您應該使用 OutputSettings 屬性,以取得有正確設定的 XmlWriterSettings 物件。If the XmlWriter is being used with the Transform(String, XmlWriter) method, you should use the OutputSettings property to obtain an XmlWriterSettings object with the correct settings. 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。This ensures that the created XmlWriter object has the correct output settings.

傳回

XmlWriter

XmlWriter 物件。An XmlWriter object.

例外狀況

text 值為 nullThe text value is null.

範例

下列範例會寫出 XML 字串。The following example writes out an XML string.

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
StringWriter sw = new StringWriter();

using (XmlWriter writer = XmlWriter.Create(sw, settings))
{
    writer.WriteStartElement("book");
    writer.WriteElementString("price", "19.95");
    writer.WriteEndElement();
    writer.Flush();

    String output = sw.ToString();
}

Dim settings As New XmlWriterSettings()
settings.OmitXmlDeclaration = True
Dim sw As New StringWriter()
        
Using writer As XmlWriter = XmlWriter.Create(sw, settings)
  writer.WriteStartElement("book")
  writer.WriteElementString("price", "19.95")
  writer.WriteEndElement()
  writer.Flush()
            
  Dim output As String = sw.ToString()
End Using

適用於

Create(Stream, XmlWriterSettings)

使用資料流和 XmlWriter 物件,建立新 XmlWriterSettings 執行個體。Creates a new XmlWriter instance using the stream and XmlWriterSettings object.

public:
 static System::Xml::XmlWriter ^ Create(System::IO::Stream ^ output, System::Xml::XmlWriterSettings ^ settings);
public static System.Xml.XmlWriter Create (System.IO.Stream output, System.Xml.XmlWriterSettings settings);
public static System.Xml.XmlWriter Create (System.IO.Stream output, System.Xml.XmlWriterSettings? settings);
static member Create : System.IO.Stream * System.Xml.XmlWriterSettings -> System.Xml.XmlWriter
Public Shared Function Create (output As Stream, settings As XmlWriterSettings) As XmlWriter

參數

output
Stream

要寫入其中的資料流。The stream to which you want to write. XmlWriter 會寫入 XML 1.0 文字語法,並將其附加至指定的資料流。The XmlWriter writes XML 1.0 text syntax and appends it to the specified stream.

settings
XmlWriterSettings

用於設定新 XmlWriterSettings 執行個體的 XmlWriter 物件。The XmlWriterSettings object used to configure the new XmlWriter instance. 如果是 null,則會使用有預設值的 XmlWriterSettingsIf this is null, a XmlWriterSettings with default settings is used.

如果 XmlWriter 正配合 Transform(String, XmlWriter) 方法使用,您應該使用 OutputSettings 屬性,以取得有正確設定的 XmlWriterSettings 物件。If the XmlWriter is being used with the Transform(String, XmlWriter) method, you should use the OutputSettings property to obtain an XmlWriterSettings object with the correct settings. 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。This ensures that the created XmlWriter object has the correct output settings.

傳回

XmlWriter

XmlWriter 物件。An XmlWriter object.

例外狀況

stream 值為 nullThe stream value is null.

範例

下列範例會將 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 additional 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 additional processing on the stream.

備註

XmlWriter 一律會將位元組順序標記 (BOM) 寫入基礎資料流程;不過,某些資料流程不能有 BOM。XmlWriter always writes a Byte Order Mark (BOM) to the underlying data stream; however, some streams must not have a BOM. 若要省略 BOM,請建立新的物件,並將 [ XmlWriterSettings 編碼] 屬性設定為新的物件,並將 [函式] UTF8Encoding 中的布林值設定為 [false]。To omit the BOM, create a new XmlWriterSettings object and set the Encoding property to be a new UTF8Encoding object with the Boolean value in the constructor set to false.

適用於

Create(XmlWriter, XmlWriterSettings)

使用指定的 XmlWriterXmlWriter 物件,建立新的 XmlWriterSettings 執行個體。Creates a new XmlWriter instance using the specified XmlWriter and XmlWriterSettings objects.

public:
 static System::Xml::XmlWriter ^ Create(System::Xml::XmlWriter ^ output, System::Xml::XmlWriterSettings ^ settings);
public static System.Xml.XmlWriter Create (System.Xml.XmlWriter output, System.Xml.XmlWriterSettings settings);
public static System.Xml.XmlWriter Create (System.Xml.XmlWriter output, System.Xml.XmlWriterSettings? settings);
static member Create : System.Xml.XmlWriter * System.Xml.XmlWriterSettings -> System.Xml.XmlWriter
Public Shared Function Create (output As XmlWriter, settings As XmlWriterSettings) As XmlWriter

參數

output
XmlWriter

您想要當做基礎寫入器使用的 XmlWriter 物件。The XmlWriter object that you want to use as the underlying writer.

settings
XmlWriterSettings

用於設定新 XmlWriterSettings 執行個體的 XmlWriter 物件。The XmlWriterSettings object used to configure the new XmlWriter instance. 如果是 null,則會使用有預設值的 XmlWriterSettingsIf this is null, a XmlWriterSettings with default settings is used.

如果 XmlWriter 正配合 Transform(String, XmlWriter) 方法使用,您應該使用 OutputSettings 屬性,以取得有正確設定的 XmlWriterSettings 物件。If the XmlWriter is being used with the Transform(String, XmlWriter) method, you should use the OutputSettings property to obtain an XmlWriterSettings object with the correct settings. 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。This ensures that the created XmlWriter object has the correct output settings.

傳回

XmlWriter

XmlWriter 物件,包裝於指定的 XmlWriter 物件附近。An XmlWriter object that is wrapped around the specified XmlWriter object.

例外狀況

writer 值為 nullThe writer value is null.

備註

這個方法可讓您將其他功能新增至基礎 XmlWriter 物件。This method allows you add additional features to an underlying XmlWriter object. 基礎 XmlWriter 物件可以是方法所建立的物件 XmlWriter.Create ,或使用執行所建立的物件 XmlTextWriterThe underlying XmlWriter object can be an object created by the XmlWriter.Create method, or an object created using the XmlTextWriter implementation.

適用於

Create(StringBuilder)

使用指定的 XmlWriter,建立新 StringBuilder 執行個體。Creates a new XmlWriter instance using the specified StringBuilder.

public:
 static System::Xml::XmlWriter ^ Create(System::Text::StringBuilder ^ output);
public static System.Xml.XmlWriter Create (System.Text.StringBuilder output);
static member Create : System.Text.StringBuilder -> System.Xml.XmlWriter
Public Shared Function Create (output As StringBuilder) As XmlWriter

參數

output
StringBuilder

要寫入至其中的 StringBuilderThe StringBuilder to which to write to. XmlWriter 寫入的內容會附加至 StringBuilderContent written by the XmlWriter is appended to the StringBuilder.

傳回

XmlWriter

XmlWriter 物件。An XmlWriter object.

例外狀況

builder 值為 nullThe builder value is null.

備註

當您使用此多載時, XmlWriterSettings 會使用具有預設設定的物件來建立 XML 寫入器。When you use this overload, an XmlWriterSettings object with default settings is used to create the XML writer.

設定Setting 預設Default
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格Two spaces
NamespaceHandling Default 不 (移除) Default (no removal)
NewLineChars \r\n (歸位字元,新行)\r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果您想要指定要在建立的 XML 寫入器上支援的功能,請使用多載,將 XmlWriterSettings 物件做為其引數的其中一個,並傳入 XmlWriterSettings 具有您自訂設定的物件。If you want to specify the features to support on the created XML writer, use an overload that takes an XmlWriterSettings object as one of its arguments, and pass in a XmlWriterSettings object with your custom settings.

適用於

Create(String)

使用指定的檔名,建立新 XmlWriter 執行個體。Creates a new XmlWriter instance using the specified filename.

public:
 static System::Xml::XmlWriter ^ Create(System::String ^ outputFileName);
public static System.Xml.XmlWriter Create (string outputFileName);
static member Create : string -> System.Xml.XmlWriter
Public Shared Function Create (outputFileName As String) As XmlWriter

參數

outputFileName
String

要寫入其中的檔案。The file to which you want to write. XmlWriter 會於指定路徑建立檔案,並在 XML 1.0 文字語法中寫入它。The XmlWriter creates a file at the specified path and writes to it in XML 1.0 text syntax. outputFileName 必須是檔案系統路徑。The outputFileName must be a file system path.

傳回

XmlWriter

XmlWriter 物件。An XmlWriter object.

例外狀況

url 值為 nullThe url value is null.

範例

下列範例會建立 XmlWriter 物件並寫入書籍節點。The following example creates an XmlWriter object and writes a book node.

using (XmlWriter writer = XmlWriter.Create("output.xml"))
{
    writer.WriteStartElement("book");
    writer.WriteElementString("price", "19.95");
    writer.WriteEndElement();
    writer.Flush();
}

Using writer As XmlWriter = XmlWriter.Create("output.xml")
  writer.WriteStartElement("book")
  writer.WriteElementString("price", "19.95")
  writer.WriteEndElement()
  writer.Flush()
End Using

備註

當您使用此多載時, XmlWriterSettings 會使用具有預設設定的物件來建立 XML 寫入器。When you use this overload, an XmlWriterSettings object with default settings is used to create the XML writer.

設定Setting 預設Default
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格Two spaces
NamespaceHandling Default 不 (移除) Default (no removal)
NewLineChars \r\n (歸位字元,新行)\r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果您想要指定要在建立的 XML 寫入器上支援的功能,請使用多載,將 XmlWriterSettings 物件做為其引數的其中一個,並傳入 XmlWriterSettings 具有您自訂設定的物件。If you want to specify the features to support on the created XML writer, use an overload that takes an XmlWriterSettings object as one of its arguments, and pass in a XmlWriterSettings object with your custom settings.

適用於

Create(TextWriter)

使用指定的 XmlWriter,建立新 TextWriter 執行個體。Creates a new XmlWriter instance using the specified TextWriter.

public:
 static System::Xml::XmlWriter ^ Create(System::IO::TextWriter ^ output);
public static System.Xml.XmlWriter Create (System.IO.TextWriter output);
static member Create : System.IO.TextWriter -> System.Xml.XmlWriter
Public Shared Function Create (output As TextWriter) As XmlWriter

參數

output
TextWriter

要寫入至其中的 TextWriterThe TextWriter to which you want to write. XmlWriter 會寫入 XML 1.0 文字語法,並將其附加至指定的 TextWriterThe XmlWriter writes XML 1.0 text syntax and appends it to the specified TextWriter.

傳回

XmlWriter

XmlWriter 物件。An XmlWriter object.

例外狀況

text 值為 nullThe text value is null.

範例

下列範例會建立輸出至主控台的寫入器。The following example creates a writer that outputs to the console.

using (XmlWriter writer = XmlWriter.Create(Console.Out))
{
    writer.WriteStartElement("book");
    writer.WriteElementString("price", "19.95");
    writer.WriteEndElement();
    writer.Flush();
}

Using writer As XmlWriter = XmlWriter.Create(Console.Out)
  writer.WriteStartElement("book")
  writer.WriteElementString("price", "19.95")
  writer.WriteEndElement()
  writer.Flush()
End Using

備註

當您使用此多載時, XmlWriterSettings 會使用具有預設設定的物件來建立 XML 寫入器。When you use this overload, an XmlWriterSettings object with default settings is used to create the XML writer.

設定Setting 預設Default
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格Two spaces
NamespaceHandling Default 不 (移除) Default (no removal)
NewLineChars \r\n (歸位字元,新行)\r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果您想要指定要在建立的寫入器上支援的功能,請使用會將 XmlWriterSettings 物件做為其引數的多載,並使用 XmlWriterSettings 您的自訂設定傳入物件。If you want to specify the features to support on the created writer, use an overload that takes an XmlWriterSettings object as one of its arguments, and pass in an XmlWriterSettings object with your custom settings.

適用於

Create(Stream)

使用指定的資料流,建立新的 XmlWriter 執行個體。Creates a new XmlWriter instance using the specified stream.

public:
 static System::Xml::XmlWriter ^ Create(System::IO::Stream ^ output);
public static System.Xml.XmlWriter Create (System.IO.Stream output);
static member Create : System.IO.Stream -> System.Xml.XmlWriter
Public Shared Function Create (output As Stream) As XmlWriter

參數

output
Stream

要寫入其中的資料流。The stream to which you want to write. XmlWriter 會寫入 XML 1.0 文字語法,並將其附加至指定的資料流。The XmlWriter writes XML 1.0 text syntax and appends it to the specified stream.

傳回

XmlWriter

XmlWriter 物件。An XmlWriter object.

例外狀況

stream 值為 nullThe stream value is null.

範例

下列範例會將 XML 片段寫入至記憶體資料流程。The following example writes an XML fragment to a memory stream. (它會使用多載 Create(Stream, XmlWriterSettings) ,這也會在新的 XML 寫入器實例上進行設定。 ) (It uses the Create(Stream, XmlWriterSettings) overload, which also configures the settings on the new XML writer instance.)

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 additional 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 additional processing on the stream.

備註

當您使用此多載時, XmlWriterSettings 會使用具有下列預設設定的物件來建立 XML 寫入器:When you use this overload, an XmlWriterSettings object with the following default settings is used to create the XML writer:

設定Setting 預設Default
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格Two spaces
NamespaceHandling Default 不 (移除) Default (no removal)
NewLineChars \r\n (歸位字元,新行)\r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果您想要指定要在建立的寫入器上支援的功能,請使用會將 XmlWriterSettings 物件做為其引數的多載,並使用 XmlWriterSettings 您的自訂設定傳入物件。If you want to specify the features to support on the created writer, use an overload that takes an XmlWriterSettings object as one of its arguments, and pass in an XmlWriterSettings object with your custom settings.

此外,XmlWriter 一律會將位元組順序標記 (BOM) 寫入基礎資料流程;不過,某些資料流程不能有 BOM。Also, XmlWriter always writes a Byte Order Mark (BOM) to the underlying data stream; however, some streams must not have a BOM. 若要省略 BOM,請建立新的物件,並將 [ XmlWriterSettings 編碼] 屬性設定為新的物件,並將 [函式] UTF8Encoding 中的布林值設定為 [false]。To omit the BOM, create a new XmlWriterSettings object and set the Encoding property to be a new UTF8Encoding object with the Boolean value in the constructor set to false.

適用於

Create(XmlWriter)

使用指定的 XmlWriter 物件,建立新 XmlWriter 執行個體。Creates a new XmlWriter instance using the specified XmlWriter object.

public:
 static System::Xml::XmlWriter ^ Create(System::Xml::XmlWriter ^ output);
public static System.Xml.XmlWriter Create (System.Xml.XmlWriter output);
static member Create : System.Xml.XmlWriter -> System.Xml.XmlWriter
Public Shared Function Create (output As XmlWriter) As XmlWriter

參數

output
XmlWriter

您想要當做基礎寫入器使用的 XmlWriter 物件。The XmlWriter object that you want to use as the underlying writer.

傳回

XmlWriter

XmlWriter 物件,包裝於指定的 XmlWriter 物件附近。An XmlWriter object that is wrapped around the specified XmlWriter object.

例外狀況

writer 值為 nullThe writer value is null.

備註

這個方法可讓您將功能新增至基礎 XmlWriter 物件。This method allows you add features to an underlying XmlWriter object. 基礎 XmlWriter 物件可以是方法所建立的物件 XmlWriter.Create ,或使用執行所建立的物件 XmlTextWriterThe underlying XmlWriter object can be an object created by the XmlWriter.Create method, or an object created using the XmlTextWriter implementation.

當您使用此多載時, XmlWriterSettings 會使用具有預設設定的物件來建立 XML 寫入器。When you use this overload, an XmlWriterSettings object with default settings is used to create the XML writer.

設定Setting 預設Default
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格Two spaces
NamespaceHandling Default 不 (移除) Default (no removal)
NewLineChars \r\n (歸位字元,新行)\r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果您想要指定要在建立的 XML 寫入器上支援的功能,請使用多載,將 XmlWriterSettings 物件做為其引數的其中一個,並傳入 XmlWriterSettings 具有您自訂設定的物件。If you want to specify the features to support on the created XML writer, use an overload that takes an XmlWriterSettings object as one of its arguments, and pass in a XmlWriterSettings object with your custom settings.

適用於