XmlWriter.Create 方法

定义

创建一个新的 XmlWriter 实例。Creates a new XmlWriter instance.

重载

Create(XmlWriter, XmlWriterSettings)

使用指定的 XmlWriterXmlWriter 对象创建一个新的 XmlWriterSettings 实例。Creates a new XmlWriter instance using the specified XmlWriter 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(StringBuilder, XmlWriterSettings)

使用 XmlWriterStringBuilder 对象创建一个新的 XmlWriterSettings 实例。Creates a new XmlWriter instance using the StringBuilder 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.

注解

某些重载包含接受XmlWriterSettings对象的参数。settings 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.

  • 充分利用功能,如更好的一致性检查和符合XML 1.0 建议,这些建议仅XmlWriter可用于静态Create方法创建的对象。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)

使用指定的 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 对象。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 对象周围的包装。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由方法创建的对象,也可以是使用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)

使用文件名和 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

要对其写入的文件。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 对象。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);
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 对象。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);
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 对象。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 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的对象,并将 "编码" 属性设置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)

使用 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

要写入的 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 对象。An XmlWriter object.

异常

builder 值为 nullThe builder value is null.

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 对象。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 对象。An XmlWriter object.

异常

url 值为 nullThe url value is null.

示例

下面的示例创建一个XmlWriter对象并写入一个 book 节点。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 对象。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 对象。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 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的对象,并将 "编码" 属性设置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 对象周围的包装。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由方法创建的对象,也可以是使用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.

适用于