XmlReaderCreate Method
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates a new XmlReader instance.

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

NameDescription
System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance using the specified stream with default settings.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance with the specified stream and settings.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance using the specified stream, base URI, and settings.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance using the specified stream, settings, and context information for parsing.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance with specified URI.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance by using the specified URI and settings.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance by using the specified URI, settings, and context information for parsing.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance by using the specified text reader.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance by using the specified text reader and settings.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance by using the specified text reader, settings, and base URI.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance by using the specified text reader, settings, and context information for parsing.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new XmlReader instance by using the specified XML reader and settings.

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

  • Specify which features you want supported on the XmlReader object.

  • Reuse the XmlReaderSettings object to create multiple readers. You can use the same settings to create multiple readers with the same functionality. Or, you can modify the settings on an XmlReaderSettingsinstance and create a new reader with a different set of features.

  • Add features to an existing XML reader. The Create method can accept another XmlReader object. The underlying XmlReader object can be a user-defined reader, a XmlTextReader object, or another XmlReader instance that you want to add additional features to.

  • Take full advantage of features such as better conformance checking and compliance to the XML 1.0 (fourth edition)recommendation that are available only on XmlReader objects created by the static Create method.

System_CAPS_noteNote

Although the .NET Framework includes concrete implementations of the XmlReader class, such as the XmlTextReader, XmlNodeReader, and the XmlValidatingReader classes, we recommend that you create XmlReader instances by using the Create method.

If you use a Create overload that doesn't accept a XmlReaderSettings object, the following default reader settings are used:

Setting

Default

true

ConformanceLevelDocument

false

false

false

0

0

null

Prohibit

An empty XmlSchemaSet object

ProcessIdentityConstraints enabled

None

A new XmlUrlResolver object. Starting with the .NET Framework 4.5.2, this setting has a default value of null.

Here are the XmlReaderSettings properties you should set for some of the typical XML reader scenarios.

Requirement

Set

Data must be a well-formed XML document.

to Document.

Data must be a well-formed XML parsed entity.

to Fragment.

Data must be validated against a DTD.

to Parse
to DTD.

Data must be validated against an XML schema.

to Schema
to the XmlSchemaSet to use for validation. Note that XmlReader doesn't support XML-Data Reduced (XDR) schema validation.

Data must be validated against an inline XML schema.

to Schema
to ProcessInlineSchema.

Type support.

to Schema
to the XmlSchemaSet to use.

XmlReader doesn't support XML-Data Reduced (XDR) schema validation.

In synchronous mode, the Create method reads the first chunk of data from the buffer of the file, stream, or text reader. This may throw an exception if an I/O operation fails. In asynchronous mode, the first I/O operation occurs with a read operation, so exceptions that arise will be thrown when the read operation occurs.

By default, the XmlReader uses an XmlUrlResolver object with no user credentials to open resources. This means that, by default, the XML reader can access any location that doesn't require credentials. Use the property to control access to resources:

-or-

  • Set to null to prevent the XML reader from opening any external resources.

This example creates an XML reader that strips insignificant white space, strips comments, and performs fragment-level conformance checking.

XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create("books.xml", settings);
Dim settings As New XmlReaderSettings()
settings.ConformanceLevel = ConformanceLevel.Fragment
settings.IgnoreWhitespace = true
settings.IgnoreComments = true
Dim reader As XmlReader = XmlReader.Create("books.xml", settings)

The following example uses an XmlUrlResolver with default credentials to access a file.

// Set the reader settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreWhitespace = true;
' Set the reader settings.
Dim settings as XmlReaderSettings = new XmlReaderSettings()
settings.IgnoreComments = true
settings.IgnoreProcessingInstructions = true
settings.IgnoreWhitespace = true
// Set the reader settings.
XmlReaderSettings^ settings = gcnew XmlReaderSettings;
settings->IgnoreComments = true;
settings->IgnoreProcessingInstructions = true;
settings->IgnoreWhitespace = true;
// Create a resolver with default credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Set the reader settings object to use the resolver.
settings.XmlResolver = resolver;

// Create the XmlReader object.
XmlReader reader = XmlReader.Create("http://ServerName/data/books.xml", settings);
' Create a resolver with default credentials.
Dim resolver as XmlUrlResolver = new XmlUrlResolver()
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

' Set the reader settings object to use the resolver.
settings.XmlResolver = resolver

' Create the XmlReader object.
Dim reader as XmlReader = XmlReader.Create("http://ServerName/data/books.xml", settings)
// Create a resolver with default credentials.
XmlUrlResolver^ resolver = gcnew XmlUrlResolver;
resolver->Credentials = System::Net::CredentialCache::DefaultCredentials;

 // Set the reader settings object to use the resolver.
 settings->XmlResolver = resolver;

// Create the XmlReader object.
XmlReader^ reader = XmlReader::Create( L"http://ServerName/data/books.xml", settings );

The following code wraps a reader instance within another reader.

XmlTextReader txtReader = new XmlTextReader("bookOrder.xml");
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("urn:po-schema", "PO.xsd");
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create(txtReader, settings);
Dim txtReader As XmlTextReader = New XmlTextReader("bookOrder.xml")
Dim settings As New XmlReaderSettings()
settings.Schemas.Add("urn:po-schema", "PO.xsd")
settings.ValidationType = ValidationType.Schema
Dim reader As XmlReader = XmlReader.Create(txtReader, settings)

This example chains readers to add DTD and XML schema validation.

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.DTD;
XmlReader inner = XmlReader.Create("book.xml", settings); // DTD Validation
settings.Schemas.Add("urn:book-schema", "book.xsd");
settings.ValidationType = ValidationType.Schema;
XmlReader outer = XmlReader.Create(inner, settings);  // XML Schema Validation
Dim settings As New XmlReaderSettings()
settings.ValidationType = ValidationType.DTD
Dim inner As XmlReader = XmlReader.Create("book.xml", settings) ' DTD Validation
settings.Schemas.Add("urn:book-schema", "book.xsd")
settings.ValidationType = ValidationType.Schema
Dim outer As XmlReader = XmlReader.Create(inner, settings)  ' XML Schema Validation
Return to top