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

Definition

Creates a new XmlWriter instance.

Overloads

Create(XmlWriter, XmlWriterSettings) Create(XmlWriter, XmlWriterSettings) Create(XmlWriter, XmlWriterSettings) Create(XmlWriter, 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)

Creates a new XmlWriter instance using the filename and XmlWriterSettings object.

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

Creates a new XmlWriter instance using the TextWriter and XmlWriterSettings objects.

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

Creates a new XmlWriter instance using the stream and XmlWriterSettings object.

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

Creates a new XmlWriter instance using the StringBuilder and XmlWriterSettings objects.

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

Creates a new XmlWriter instance using the specified StringBuilder.

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

Creates a new XmlWriter instance using the specified filename.

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

Creates a new XmlWriter instance using the specified TextWriter.

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

Creates a new XmlWriter instance using the specified stream.

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

Creates a new XmlWriter instance using the specified XmlWriter object.

Remarks

Some of the Create overloads include a settings parameter that accepts an XmlWriterSettings object. You can use this object to:

  • Specify which features you want supported on the created XmlWriter object.

  • Reuse the XmlWriterSettings object to create multiple writer objects. The XmlWriterSettings object is copied and marked read-only for each created writer. 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. Or, you can modify the settings on an XmlWriterSettings instance and create a new writer with a different set of features.

  • Add features to an existing XML writer. The Create method can accept another XmlWriter object. The underlying XmlWriter object does not have to be an XML writer created by the static Create method. For example, you can specify a user-defined XML writer to add additional features to.

  • 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.

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 (no removal)
NewLineChars \r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

Note

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)

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
Parameters
output
XmlWriter XmlWriter XmlWriter XmlWriter

The XmlWriter object that you want to use as the underlying writer.

settings
XmlWriterSettings XmlWriterSettings XmlWriterSettings XmlWriterSettings

The XmlWriterSettings object used to configure the new XmlWriter instance. If this is null, a XmlWriterSettings with default settings is used.

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. This ensures that the created XmlWriter object has the correct output settings.

Returns

An XmlWriter object that is wrapped around the specified XmlWriter object.

Exceptions

Remarks

This method allows you add additional features to an underlying XmlWriter object. 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)

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
Parameters
outputFileName
String String String String

The file to which you want to write. The XmlWriter creates a file at the specified path and writes to it in XML 1.0 text syntax. The outputFileName must be a file system path.

settings
XmlWriterSettings XmlWriterSettings XmlWriterSettings XmlWriterSettings

The XmlWriterSettings object used to configure the new XmlWriter instance. If this is null, a XmlWriterSettings with default settings is used.

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. This ensures that the created XmlWriter object has the correct output settings.

Returns
Exceptions

Examples

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
Imports System.IO
Imports System.Xml
Imports System.Text
Imports Microsoft.VisualBasic

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)

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
Parameters
output
TextWriter TextWriter TextWriter TextWriter

The TextWriter to which you want to write. The XmlWriter writes XML 1.0 text syntax and appends it to the specified TextWriter.

settings
XmlWriterSettings XmlWriterSettings XmlWriterSettings XmlWriterSettings

The XmlWriterSettings object used to configure the new XmlWriter instance. If this is null, a XmlWriterSettings with default settings is used.

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. This ensures that the created XmlWriter object has the correct output settings.

Returns
Exceptions

Examples

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)

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
Parameters
output
Stream Stream Stream Stream

The stream to which you want to write. The XmlWriter writes XML 1.0 text syntax and appends it to the specified stream.

settings
XmlWriterSettings XmlWriterSettings XmlWriterSettings XmlWriterSettings

The XmlWriterSettings object used to configure the new XmlWriter instance. If this is null, a XmlWriterSettings with default settings is used.

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. This ensures that the created XmlWriter object has the correct output settings.

Returns
Exceptions

Examples

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.

Remarks

XmlWriter always writes a Byte Order Mark (BOM) to the underlying data stream; however, some streams must not have a BOM. 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)

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
Parameters
output
StringBuilder StringBuilder StringBuilder StringBuilder

The StringBuilder to which to write to. Content written by the XmlWriter is appended to the StringBuilder.

settings
XmlWriterSettings XmlWriterSettings XmlWriterSettings XmlWriterSettings

The XmlWriterSettings object used to configure the new XmlWriter instance. If this is null, a XmlWriterSettings with default settings is used.

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. This ensures that the created XmlWriter object has the correct output settings.

Returns
Exceptions

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

The StringBuilder to which to write to. Content written by the XmlWriter is appended to the StringBuilder.

Returns
Exceptions

Remarks

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 (no removal)
NewLineChars \r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

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)

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
Parameters
outputFileName
String String String String

The file to which you want to write. The XmlWriter creates a file at the specified path and writes to it in XML 1.0 text syntax. The outputFileName must be a file system path.

Returns
Exceptions

Examples

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

Remarks

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 (no removal)
NewLineChars \r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

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)

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
Parameters
output
TextWriter TextWriter TextWriter TextWriter

The TextWriter to which you want to write. The XmlWriter writes XML 1.0 text syntax and appends it to the specified TextWriter.

Returns
Exceptions

Examples

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

Remarks

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 (no removal)
NewLineChars \r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

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)

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
Parameters
output
Stream Stream Stream Stream

The stream to which you want to write. The XmlWriter writes XML 1.0 text syntax and appends it to the specified stream.

Returns
Exceptions

Examples

The following example writes an XML fragment to a memory stream. (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.

Remarks

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 (no removal)
NewLineChars \r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

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.

Also, XmlWriter always writes a Byte Order Mark (BOM) to the underlying data stream; however, some streams must not have a BOM. 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)

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
Parameters
output
XmlWriter XmlWriter XmlWriter XmlWriter

The XmlWriter object that you want to use as the underlying writer.

Returns

An XmlWriter object that is wrapped around the specified XmlWriter object.

Exceptions

Remarks

This method allows you add features to an underlying XmlWriter object. The underlying XmlWriter object can be an object created by the XmlWriter.Create method, or an object created using the XmlTextWriter implementation.

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 (no removal)
NewLineChars \r\n (carriage return, new line)
NewLineHandling NewLineHandling.Replace
NewLineOnAttributes false
OmitXmlDeclaration false
OutputMethod XmlOutputMethod.Xml
WriteEndDocumentOnClose true

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.

Applies to