Export (0) Print
Expand All

XmlReader.Create Method

Creates a new XmlReader instance.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  Name Description
Public method Static member Create(Stream) Creates a new XmlReader instance using the specified stream with default settings.
Public method Static member Create(String) Creates a new XmlReader instance with specified URI.
Public method Static member Create(TextReader) Creates a new XmlReader instance by using the specified text reader.
Public method Static member Create(Stream, XmlReaderSettings) Creates a new XmlReader instance with the specified stream and settings.
Public method Static member Create(String, XmlReaderSettings) Creates a new XmlReader instance by using the specified URI and settings.
Public method Static member Create(TextReader, XmlReaderSettings) Creates a new XmlReader instance by using the specified text reader and settings.
Public method Static member Create(XmlReader, XmlReaderSettings) Creates a new XmlReader instance by using the specified XML reader and settings.
Public method Static member Create(Stream, XmlReaderSettings, String) Creates a new XmlReader instance using the specified stream, base URI, and settings.
Public method Static member Create(Stream, XmlReaderSettings, XmlParserContext) Creates a new XmlReader instance using the specified stream, settings, and context information for parsing.
Public method Static member Create(String, XmlReaderSettings, XmlParserContext) Creates a new XmlReader instance by using the specified URI, settings, and context information for parsing.
Public method Static member Create(TextReader, XmlReaderSettings, String) Creates a new XmlReader instance by using the specified text reader, settings, and base URI.
Public method Static member Create(TextReader, XmlReaderSettings, XmlParserContext) Creates a new XmlReader instance by using the specified text reader, settings, and context information for parsing.
Top

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 XmlReaderSettings instance 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.

Note Note

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.

Default settings

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

Settings for common scenarios

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.

ConformanceLevel to Document.

Data must be a well-formed XML parsed entity.

ConformanceLevel to Fragment.

Data must be validated against a DTD.

DtdProcessing to Parse
ValidationType to DTD.

Data must be validated against an XML schema.

ValidationType to Schema
Schemas 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.

ValidationType to Schema
ValidationFlags to ProcessInlineSchema.

Type support.

ValidationType to Schema
Schemas to the XmlSchemaSet to use.

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

Asynchronous programming

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.

Security considerations

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 XmlResolver property to control access to resources:

-or-

  • Set XmlResolver 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);

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;
// 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);

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);

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
Show:
© 2014 Microsoft