XmlWriter.Create 方法

定義

建立新的 XmlWriter 執行個體。

多載

Create(StringBuilder, XmlWriterSettings)

使用 XmlWriterStringBuilder 物件,建立新的 XmlWriterSettings 執行個體。

Create(String, XmlWriterSettings)

使用檔名和 XmlWriter 物件,建立新 XmlWriterSettings 執行個體。

Create(TextWriter, XmlWriterSettings)

使用 XmlWriterTextWriter 物件,建立新的 XmlWriterSettings 執行個體。

Create(Stream, XmlWriterSettings)

使用資料流和 XmlWriter 物件,建立新 XmlWriterSettings 執行個體。

Create(XmlWriter, XmlWriterSettings)

使用指定的 XmlWriterXmlWriter 物件,建立新的 XmlWriterSettings 執行個體。

Create(StringBuilder)

使用指定的 XmlWriter,建立新 StringBuilder 執行個體。

Create(String)

使用指定的檔名,建立新 XmlWriter 執行個體。

Create(TextWriter)

使用指定的 XmlWriter,建立新 TextWriter 執行個體。

Create(Stream)

使用指定的資料流,建立新的 XmlWriter 執行個體。

Create(XmlWriter)

使用指定的 XmlWriter 物件,建立新 XmlWriter 執行個體。

備註

某些 Create 多載包含 settings 可接受 XmlWriterSettings 物件的參數。 您可以使用此物件來執行下列動作:

  • 指定您想要在建立 XmlWriter 的物件上支援的功能。

  • XmlWriterSettings重複使用 物件來建立多個寫入器物件。 複製每個已建立之寫入器的 XmlWriterSettings 物件,並標示為唯讀。 對 XmlWriterSettings 執行個體上設定的變更不會影響具有相同設定的現有寫入器。 因此,您可以使用相同設定來建立具有相同功能的多個寫入器。 或者,您可以修改 XmlWriterSettings 執行個體上的設定,並建立具有一組不同功能的新寫入器。

  • 將功能新增至現有的 XML 寫入器。 Create 方法可以接受其他 XmlWriter 物件。 基礎 XmlWriter 物件不一定是靜態 Create 方法所建立的 XML 寫入器。 例如,您可以指定使用者定義的 XML 寫入器,以新增其他功能。

  • 充分利用 XML 1.0 建議 的更佳一致性檢查和合規性等功能,這些建議僅適用于 XmlWriter 靜態 Create 方法所建立的物件。

如果您使用 Create 不接受 XmlWriterSettings 物件的多載,則會使用下列預設寫入器設定:

設定 預設
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格
NamespaceHandling Default (沒有移除)
NewLineChars \r\n (歸位字元,新行)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

注意

雖然.NET Framework包含 XmlTextWriter 類別,這是 類別的具體實作 XmlWriter ,但建議您使用 Create 方法來建立 XmlWriter 實例。

Create(StringBuilder, XmlWriterSettings)

使用 XmlWriterStringBuilder 物件,建立新的 XmlWriterSettings 執行個體。

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

要寫入至其中的 StringBuilderXmlWriter 寫入的內容會附加至 StringBuilder

settings
XmlWriterSettings

用於設定新 XmlWriterSettings 執行個體的 XmlWriter 物件。 如果是 null,則會使用有預設值的 XmlWriterSettings

如果 XmlWriter 正配合 Transform(String, XmlWriter) 方法使用,您應該使用 OutputSettings 屬性,以取得有正確設定的 XmlWriterSettings 物件。 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。

傳回

XmlWriter 物件。

例外狀況

builder 值為 null

適用於

Create(String, XmlWriterSettings)

使用檔名和 XmlWriter 物件,建立新 XmlWriterSettings 執行個體。

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

要寫入其中的檔案。 XmlWriter 會於指定路徑建立檔案,並在 XML 1.0 文字語法中寫入它。 outputFileName 必須是檔案系統路徑。

settings
XmlWriterSettings

用於設定新 XmlWriterSettings 執行個體的 XmlWriter 物件。 如果是 null,則會使用有預設值的 XmlWriterSettings

如果 XmlWriter 正配合 Transform(String, XmlWriter) 方法使用,您應該使用 OutputSettings 屬性,以取得有正確設定的 XmlWriterSettings 物件。 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。

傳回

XmlWriter 物件。

例外狀況

url 值為 null

範例

下列範例會 XmlWriter 建立具有已定義設定的物件。

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 執行個體。

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

要寫入至其中的 TextWriterXmlWriter 會寫入 XML 1.0 文字語法,並將其附加至指定的 TextWriter

settings
XmlWriterSettings

用於設定新 XmlWriterSettings 執行個體的 XmlWriter 物件。 如果是 null,則會使用有預設值的 XmlWriterSettings

如果 XmlWriter 正配合 Transform(String, XmlWriter) 方法使用,您應該使用 OutputSettings 屬性,以取得有正確設定的 XmlWriterSettings 物件。 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。

傳回

XmlWriter 物件。

例外狀況

text 值為 null

範例

下列範例會寫出 XML 字串。

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 執行個體。

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

要寫入其中的資料流。 XmlWriter 會寫入 XML 1.0 文字語法,並將其附加至指定的資料流。

settings
XmlWriterSettings

用於設定新 XmlWriterSettings 執行個體的 XmlWriter 物件。 如果是 null,則會使用有預設值的 XmlWriterSettings

如果 XmlWriter 正配合 Transform(String, XmlWriter) 方法使用,您應該使用 OutputSettings 屬性,以取得有正確設定的 XmlWriterSettings 物件。 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。

傳回

XmlWriter 物件。

例外狀況

stream 值為 null

範例

下列範例會將 XML 片段寫入記憶體資料流程。

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 一律會將 Byte Order Mark (BOM) 寫入基礎資料流程;不過,某些資料流程不能有 BOM。 若要省略 BOM,請建立新的 XmlWriterSettings 物件,並將 Encoding 屬性設定為新的 UTF8Encoding 物件,並將建構函式中的布林值設定為 false。

適用於

Create(XmlWriter, XmlWriterSettings)

使用指定的 XmlWriterXmlWriter 物件,建立新的 XmlWriterSettings 執行個體。

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 物件。

settings
XmlWriterSettings

用於設定新 XmlWriterSettings 執行個體的 XmlWriter 物件。 如果是 null,則會使用有預設值的 XmlWriterSettings

如果 XmlWriter 正配合 Transform(String, XmlWriter) 方法使用,您應該使用 OutputSettings 屬性,以取得有正確設定的 XmlWriterSettings 物件。 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。

傳回

XmlWriter 物件,包裝於指定的 XmlWriter 物件附近。

例外狀況

writer 值為 null

備註

這個方法可讓您將其他功能新增至基礎 XmlWriter 物件。 基礎 XmlWriter 物件可以是 方法所 XmlWriter.Create 建立的物件,或是使用 XmlTextWriter 實作建立的物件。

適用於

Create(StringBuilder)

使用指定的 XmlWriter,建立新 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

要寫入至其中的 StringBuilderXmlWriter 寫入的內容會附加至 StringBuilder

傳回

XmlWriter 物件。

例外狀況

builder 值為 null

備註

當您使用此多載時, XmlWriterSettings 會使用具有預設設定的物件來建立 XML 寫入器。

設定 預設
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格
NamespaceHandling Default (沒有移除)
NewLineChars \r\n (歸位字元,新行)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果您想要在建立的 XML 寫入器上指定支援的功能,請使用採用 物件做為其其中一個引數的多載 XmlWriterSettings ,並使用您的自訂設定傳入 XmlWriterSettings 物件。

適用於

Create(String)

使用指定的檔名,建立新 XmlWriter 執行個體。

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

要寫入其中的檔案。 XmlWriter 會於指定路徑建立檔案,並在 XML 1.0 文字語法中寫入它。 outputFileName 必須是檔案系統路徑。

傳回

XmlWriter 物件。

例外狀況

url 值為 null

範例

下列範例會 XmlWriter 建立 物件並寫入書籍節點。

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 寫入器。

設定 預設
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格
NamespaceHandling Default (沒有移除)
NewLineChars \r\n (歸位字元,新行)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果您想要在建立的 XML 寫入器上指定支援的功能,請使用採用 物件做為其其中一個引數的多載 XmlWriterSettings ,並使用您的自訂設定傳入 XmlWriterSettings 物件。

適用於

Create(TextWriter)

使用指定的 XmlWriter,建立新 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

要寫入至其中的 TextWriterXmlWriter 會寫入 XML 1.0 文字語法,並將其附加至指定的 TextWriter

傳回

XmlWriter 物件。

例外狀況

text 值為 null

範例

下列範例會建立輸出至主控台的寫入器。

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 寫入器。

設定 預設
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格
NamespaceHandling Default (沒有移除)
NewLineChars \r\n (歸位字元,新行)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果您想要在建立的寫入器上指定支援的功能,請使用採用 XmlWriterSettings 物件作為其中一個引數的多載,並使用您的自訂設定傳入 XmlWriterSettings 物件。

適用於

Create(Stream)

使用指定的資料流,建立新的 XmlWriter 執行個體。

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

要寫入其中的資料流。 XmlWriter 會寫入 XML 1.0 文字語法,並將其附加至指定的資料流。

傳回

XmlWriter 物件。

例外狀況

stream 值為 null

範例

下列範例會將 XML 片段寫入記憶體資料流程。 (它會使用 Create(Stream, XmlWriterSettings) 多載,這也會在新的 XML 寫入器實例上設定設定。)

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 寫入器:

設定 預設
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格
NamespaceHandling Default (沒有移除)
NewLineChars \r\n (歸位字元,新行)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果您想要在建立的寫入器上指定支援的功能,請使用採用 XmlWriterSettings 物件作為其中一個引數的多載,並使用您的自訂設定傳入 XmlWriterSettings 物件。

此外,XmlWriter 一律會將 Byte Order Mark (BOM) 寫入基礎資料流程;不過,某些資料流程不能有 BOM。 若要省略 BOM,請建立新的 XmlWriterSettings 物件,並將 Encoding 屬性設定為新的 UTF8Encoding 物件,並將建構函式中的布林值設定為 false。

適用於

Create(XmlWriter)

使用指定的 XmlWriter 物件,建立新 XmlWriter 執行個體。

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 物件附近。

例外狀況

writer 值為 null

備註

這個方法可讓您將功能新增至基礎 XmlWriter 物件。 基礎 XmlWriter 物件可以是 方法所 XmlWriter.Create 建立的物件,或是使用 XmlTextWriter 實作建立的物件。

當您使用此多載時, XmlWriterSettings 會使用具有預設設定的物件來建立 XML 寫入器。

設定 預設
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空格
NamespaceHandling Default (沒有移除)
NewLineChars \r\n (歸位字元,新行)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果您想要在建立的 XML 寫入器上指定支援的功能,請使用採用 物件做為其其中一個引數的多載 XmlWriterSettings ,並使用您的自訂設定傳入 XmlWriterSettings 物件。

適用於