XmlWriter.Create XmlWriter.Create XmlWriter.Create XmlWriter.Create Method

定義

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

多載

Create(XmlWriter, XmlWriterSettings) Create(XmlWriter, XmlWriterSettings) Create(XmlWriter, XmlWriterSettings) Create(XmlWriter, XmlWriterSettings)

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

Create(String, XmlWriterSettings) Create(String, XmlWriterSettings) Create(String, XmlWriterSettings) Create(String, XmlWriterSettings)

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

Create(TextWriter, XmlWriterSettings) Create(TextWriter, XmlWriterSettings) Create(TextWriter, XmlWriterSettings) Create(TextWriter, XmlWriterSettings)

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

Create(Stream, XmlWriterSettings) Create(Stream, XmlWriterSettings) Create(Stream, XmlWriterSettings) Create(Stream, XmlWriterSettings)

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

Create(StringBuilder, XmlWriterSettings) Create(StringBuilder, XmlWriterSettings) Create(StringBuilder, XmlWriterSettings) Create(StringBuilder, XmlWriterSettings)

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

Create(StringBuilder) Create(StringBuilder) Create(StringBuilder) Create(StringBuilder)

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

Create(String) Create(String) Create(String) Create(String)

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

Create(TextWriter) Create(TextWriter) Create(TextWriter) Create(TextWriter)

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

Create(Stream) Create(Stream) Create(Stream) Create(Stream)

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

Create(XmlWriter) Create(XmlWriter) Create(XmlWriter) Create(XmlWriter)

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

備註

部分多載settings包括接受XmlWriterSettings物件的參數。 CreateSome of the Create overloads include a settings parameter that accepts an XmlWriterSettings object. 您可以使用此物件來執行下列動作:You can use this object to:

  • 指定您想要在建立XmlWriter的物件上支援的功能。Specify 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物件不一定要是由靜態Create方法所建立的 XML 寫入器。The 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.

  • 充分利用僅適用于XmlWriter靜態Create方法所建立之物件的XML 1.0 建議的功能,例如更好的一致性檢查和合規性。Take 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.

如果您使用CreateXmlWriterSettings接受物件的多載,則會使用下列預設寫入器設定: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類別的具體執行),但我們建議您使用Create方法來XmlWriter建立實例。Although 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(XmlWriter, XmlWriterSettings) Create(XmlWriter, XmlWriterSettings) Create(XmlWriter, XmlWriterSettings) 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);
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 XmlWriter XmlWriter

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

settings
XmlWriterSettings XmlWriterSettings XmlWriterSettings 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 that is wrapped around the specified XmlWriter object.

例外狀況

備註

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

Create(String, XmlWriterSettings) Create(String, XmlWriterSettings) Create(String, XmlWriterSettings) 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);
static member Create : string * System.Xml.XmlWriterSettings -> System.Xml.XmlWriter
Public Shared Function Create (outputFileName As String, settings As XmlWriterSettings) As XmlWriter

參數

outputFileName
String String String 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 XmlWriterSettings 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定義的設定來建立物件。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) Create(TextWriter, XmlWriterSettings) Create(TextWriter, XmlWriterSettings) 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);
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 TextWriter TextWriter 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 XmlWriterSettings 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.

傳回

例外狀況

範例

下列範例會寫出 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) Create(Stream, XmlWriterSettings) Create(Stream, XmlWriterSettings) 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);
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 Stream Stream 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 XmlWriterSettings 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.

傳回

例外狀況

範例

下列範例會將 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 一律會將位元組順序標記(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的物件,並將 Encoding 屬性設定為新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(StringBuilder, XmlWriterSettings) Create(StringBuilder, XmlWriterSettings) Create(StringBuilder, XmlWriterSettings) 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);
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 StringBuilder StringBuilder StringBuilder

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

settings
XmlWriterSettings XmlWriterSettings XmlWriterSettings 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.

傳回

例外狀況

Create(StringBuilder) Create(StringBuilder) Create(StringBuilder) 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 StringBuilder StringBuilder StringBuilder

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

傳回

例外狀況

備註

當您使用這個多載時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) Create(String) Create(String) 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 String String 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物件並寫入書籍節點。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) Create(TextWriter) Create(TextWriter) 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 TextWriter TextWriter 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.

傳回

例外狀況

範例

下列範例會建立輸出至主控台的寫入器。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) Create(Stream) Create(Stream) 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 Stream Stream 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.

傳回

例外狀況

範例

下列範例會將 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 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.

備註

當您使用這個多載時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的物件,並將 Encoding 屬性設定為新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) Create(XmlWriter) Create(XmlWriter) 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 XmlWriter XmlWriter

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

傳回

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

例外狀況

備註

這個方法可讓您將功能新增至XmlWriter基礎物件。This method allows you add features to an underlying XmlWriter object. 基礎XmlWriter物件可以是方法所XmlWriter.Create建立的物件,或是使用此執行所XmlTextWriter建立的物件。The 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.

適用於