XmlWriter.Create 方法

定義

建立新的 XmlWriter 實例。

多載

名稱 Description
Create(XmlWriter, XmlWriterSettings)

使用指定的 XmlWriterXmlWriterSettings 物件建立一個新XmlWriter實例。

Create(String, XmlWriterSettings)

用檔名和XmlWriterSettings物件建立一個新XmlWriter實例。

Create(TextWriter, XmlWriterSettings)

TextWriterXmlWriterSettings 物件建立一個新XmlWriter實例。

Create(Stream, XmlWriterSettings)

用串流和XmlWriterSettings物件建立一個新XmlWriter實例。

Create(StringBuilder, XmlWriterSettings)

StringBuilderXmlWriterSettings 物件建立一個新XmlWriter實例。

Create(StringBuilder)

使用指定的 StringBuilder建立一個新XmlWriter實例。

Create(String)

使用指定的檔名建立一個新 XmlWriter 實例。

Create(TextWriter)

使用指定的 TextWriter建立一個新XmlWriter實例。

Create(Stream)

使用指定的串流建立一個新 XmlWriter 實例。

Create(XmlWriter)

使用指定的XmlWriter物件建立一個新XmlWriter實例。

備註

部分 Create 超載包含 settings 一個接受 XmlWriterSettings 物件的參數。 您可以使用這個物件來:

  • 指定你希望在建立物件 XmlWriter 上支援哪些功能。

  • 重複使用該 XmlWriterSettings 物件來建立多個寫入物件。 XmlWriterSettings物件會被複製,並標記為每個建立的寫入者只讀。 實例設定的變更 XmlWriterSettings 不會影響使用相同設定的現有作者。 因此,你可以用相同的設定建立多個具有相同功能的寫入者。 或者,你也可以修改實 XmlWriterSettings 例的設定,建立一個擁有不同功能的新寫手。

  • 為現有的 XML 編譯器新增功能。 方法 Create 可以接受另一個 XmlWriter 物件。 底層 XmlWriter 物件不必是靜態 Create 方法所建立的 XML 編譯器。 例如,你可以指定一個使用者自訂的 XML 編譯器,以加入額外功能。

  • 充分利用僅XmlWriter在靜態Create方法建立物件上提供的功能,如更好的符合性檢查及符合 XML 1.0 建議

如果你使用 Create 不接受物件 XmlWriterSettings 的超載,則會使用以下預設的寫入設定:

Setting 預設值
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空間
NamespaceHandling Default (無移除)
NewLineChars \r\n (回車,換行)用於非 Unix 平台,或 \n (換行)用於 Unix 平台
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

Note

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

Create(XmlWriter, XmlWriterSettings)

使用指定的 XmlWriterXmlWriterSettings 物件建立一個新XmlWriter實例。

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

例外狀況

outputnull

備註

此方法允許你為底層 XmlWriter 物件添加額外功能。 底層 XmlWriter 物件可以是方法 XmlWriter.Create 所建立的物件,或是使用實 XmlTextWriter 作建立的物件。

適用於

Create(String, XmlWriterSettings)

用檔名和XmlWriterSettings物件建立一個新XmlWriter實例。

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

你想寫入的檔案。 在 XmlWriter 指定的路徑建立檔案,並以 XML 1.0 文字語法寫入。 這 outputFileName 一定是檔案系統路徑。

settings
XmlWriterSettings

XmlWriterSettings用來配置新XmlWriter實例的物件。 如果是null,則使用預設設定的 。XmlWriterSettings

如果是用這個 XmlWriter 方法, Transform(String, XmlWriter) 你應該用這個 OutputSettings 屬性來取得 XmlWriterSettings 一個設定正確的物件。 這確保所建立 XmlWriter 的物件擁有正確的輸出設定。

傳回

XmlWriter 物件。

例外狀況

outputFileNamenull

範例

以下範例建立 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)

TextWriterXmlWriterSettings 物件建立一個新XmlWriter實例。

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 。 它 XmlWriter 會寫入 XML 1.0 文字語法,並將其附加到指定的 TextWriter

settings
XmlWriterSettings

XmlWriterSettings用來配置新XmlWriter實例的物件。 如果是null,則使用預設設定的 。XmlWriterSettings

如果是用這個 XmlWriter 方法, Transform(String, XmlWriter) 你應該用這個 OutputSettings 屬性來取得 XmlWriterSettings 一個設定正確的物件。 這確保所建立 XmlWriter 的物件擁有正確的輸出設定。

傳回

XmlWriter 物件。

例外狀況

outputnull

範例

以下範例寫出一個 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)

用串流和XmlWriterSettings物件建立一個新XmlWriter實例。

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

例外狀況

outputnull

範例

以下範例將 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 總是會寫入底層資料流的位元組順序標記(BOM);但有些串流必須沒有BOM。 要省略 BOM,請建立一個新 XmlWriterSettings 物件,並將編碼屬性設為新 UTF8Encoding 物件,且建構子中的布林值設為 false。

適用於

Create(StringBuilder, XmlWriterSettings)

StringBuilderXmlWriterSettings 物件建立一個新XmlWriter實例。

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該寫信給哪裡。 由 撰寫 XmlWriter 的內容會附加在 StringBuilder.

settings
XmlWriterSettings

XmlWriterSettings用來配置新XmlWriter實例的物件。 如果是null,則使用預設設定的 。XmlWriterSettings

如果是用這個 XmlWriter 方法, Transform(String, XmlWriter) 你應該用這個 OutputSettings 屬性來取得 XmlWriterSettings 一個設定正確的物件。 這確保所建立 XmlWriter 的物件擁有正確的輸出設定。

傳回

XmlWriter 物件。

例外狀況

outputnull

適用於

Create(StringBuilder)

使用指定的 StringBuilder建立一個新XmlWriter實例。

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該寫信給哪裡。 由 撰寫 XmlWriter 的內容會附加在 StringBuilder.

傳回

XmlWriter 物件。

例外狀況

outputnull

備註

當你使用這個超載時, XmlWriterSettings 會用一個預設設定的物件來建立 XML 編寫器。

Setting 預設值
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空間
NamespaceHandling Default (無移除)
NewLineChars \r\n (回車,換行)用於非 Unix 平台,或 \n (換行)用於 Unix 平台
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 物件。

例外狀況

outputFileNamenull

範例

以下範例建立一個 XmlWriter 物件並寫入一個 book 節點。

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

Setting 預設值
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空間
NamespaceHandling Default (無移除)
NewLineChars \r\n (回車,換行)用於非 Unix 平台,或 \n (換行)用於 Unix 平台
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果你想指定要支援的 XML 寫入器的功能,可以用一個過載工具,將 XmlWriterSettings 物件當作參數之一,然後用自訂設定傳入 XmlWriterSettings 物件。

適用於

Create(TextWriter)

使用指定的 TextWriter建立一個新XmlWriter實例。

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 。 它 XmlWriter 會寫入 XML 1.0 文字語法,並將其附加到指定的 TextWriter

傳回

XmlWriter 物件。

例外狀況

outputnull

範例

以下範例建立一個寫入器,輸出到主控台。

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

Setting 預設值
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空間
NamespaceHandling Default (無移除)
NewLineChars \r\n (回車,換行)用於非 Unix 平台,或 \n (換行)用於 Unix 平台
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 物件。

例外狀況

outputnull

範例

以下範例將 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 編入器:

Setting 預設值
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空間
NamespaceHandling Default (無移除)
NewLineChars \r\n (回車,換行)用於非 Unix 平台,或 \n (換行)用於 Unix 平台
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果你想指定要支援的寫入器功能,可以用一個超載方式,將物件 XmlWriterSettings 當作參數之一,然後用自訂設定傳入物件 XmlWriterSettings

此外,XmlWriter 總是會寫入底層資料流的位元組順序標記(BOM);但有些串流必須沒有BOM。 要省略 BOM,請建立一個新 XmlWriterSettings 物件,並將編碼屬性設為新 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 物件上的物件。

例外狀況

outputnull

備註

此方法允許您在底層 XmlWriter 物件中新增特徵。 底層 XmlWriter 物件可以是方法 XmlWriter.Create 所建立的物件,或是使用實 XmlTextWriter 作建立的物件。

當你使用這個超載時, XmlWriterSettings 會用一個預設設定的物件來建立 XML 編寫器。

Setting 預設值
CheckCharacters true
CloseOutput false
ConformanceLevel ConformanceLevel.Document
Encoding Encoding.UTF8
Indent false
IndentChars 兩個空間
NamespaceHandling Default (無移除)
NewLineChars \r\n (回車,換行)用於非 Unix 平台,或 \n (換行)用於 Unix 平台
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

如果你想指定要支援的 XML 寫入器的功能,可以用一個過載工具,將 XmlWriterSettings 物件當作參數之一,然後用自訂設定傳入 XmlWriterSettings 物件。

適用於