XmlWriter.Create Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a new XmlWriter instance.
Overloads
Create(StringBuilder, XmlWriterSettings) |
Creates a new XmlWriter instance using the StringBuilder and XmlWriterSettings objects. |
Create(String, XmlWriterSettings) |
Creates a new XmlWriter instance using the filename and XmlWriterSettings object. |
Create(TextWriter, XmlWriterSettings) |
Creates a new XmlWriter instance using the TextWriter and XmlWriterSettings objects. |
Create(Stream, XmlWriterSettings) |
Creates a new XmlWriter instance using the stream and XmlWriterSettings object. |
Create(XmlWriter, XmlWriterSettings) |
Creates a new XmlWriter instance using the specified XmlWriter and XmlWriterSettings objects. |
Create(StringBuilder) |
Creates a new XmlWriter instance using the specified StringBuilder. |
Create(String) |
Creates a new XmlWriter instance using the specified filename. |
Create(TextWriter) |
Creates a new XmlWriter instance using the specified TextWriter. |
Create(Stream) |
Creates a new XmlWriter instance using the specified stream. |
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, line feed) for non-Unix platforms, or \n (line feed) for Unix platforms |
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(StringBuilder, XmlWriterSettings)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
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);
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
The StringBuilder to which to write to. Content written by the XmlWriter is appended to the StringBuilder.
- settings
- 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.
Exceptions
The builder
value is null
.
Applies to
Create(String, XmlWriterSettings)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
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);
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
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
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.
Exceptions
The url
value is null
.
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.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
Applies to
Create(TextWriter, XmlWriterSettings)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
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);
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
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
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.
Exceptions
The text
value is null
.
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
Applies to
Create(Stream, XmlWriterSettings)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
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);
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
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
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.
Exceptions
The stream
value is null
.
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 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.
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.
Applies to
Create(XmlWriter, XmlWriterSettings)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
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);
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
- settings
- 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
The writer
value is null
.
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.
Applies to
Create(StringBuilder)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
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
The StringBuilder to which to write to. Content written by the XmlWriter is appended to the StringBuilder.
Returns
An XmlWriter object.
Exceptions
The builder
value is null
.
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, line feed) for non-Unix platforms, or \n (line feed) for Unix platforms |
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
Create(String)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
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
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
An XmlWriter object.
Exceptions
The url
value is null
.
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, line feed) for non-Unix platforms, or \n (line feed) for Unix platforms |
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
Create(TextWriter)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
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
The TextWriter to which you want to write. The XmlWriter writes XML 1.0 text syntax and appends it to the specified TextWriter.
Returns
An XmlWriter object.
Exceptions
The text
value is null
.
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, line feed) for non-Unix platforms, or \n (line feed) for Unix platforms |
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.
Applies to
Create(Stream)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
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
The stream to which you want to write. The XmlWriter writes XML 1.0 text syntax and appends it to the specified stream.
Returns
An XmlWriter object.
Exceptions
The stream
value is null
.
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 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.
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, line feed) for non-Unix platforms, or \n (line feed) for Unix platforms |
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.
Applies to
Create(XmlWriter)
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
- Source:
- XmlWriter.cs
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
Returns
An XmlWriter object that is wrapped around the specified XmlWriter object.
Exceptions
The writer
value is null
.
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, line feed) for non-Unix platforms, or \n (line feed) for Unix platforms |
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.