XmlReader.Create Method (XmlReader, XmlReaderSettings)

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

Namespace:  System.Xml
Assemblies:   System.Xml (in System.Xml.dll)
  System.Xml.ReaderWriter (in System.Xml.ReaderWriter.dll)

public static XmlReader Create(
	XmlReader reader,
	XmlReaderSettings settings
)

Parameters

reader
Type: System.Xml.XmlReader

The object that you want to use as the underlying XML reader.

settings
Type: System.Xml.XmlReaderSettings

The settings for the new XmlReader instance.

The conformance level of the XmlReaderSettings object must either match the conformance level of the underlying reader, or it must be set to Auto.

Return Value

Type: System.Xml.XmlReader
An object that is wrapped around the specified XmlReader object.

ExceptionCondition
ArgumentNullException

The reader value is null.

InvalidOperationException

If the XmlReaderSettings object specifies a conformance level that is not consistent with conformance level of the underlying reader.

-or-

The underlying XmlReader is in an Error or Closed state.

This method allows you add additional features to an underlying XmlReader object. The underlying XmlReader object can be another XmlReader object created by the Create method, or an XmlReader object created using one of the concrete XmlReader implementations.

A default XmlUrlResolver with no credentials is used to access any external resources such as a schema.

Important noteImportant

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If you’re solution targets the.NET Framework 4.5.2, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

If the external resource is located on a network resource that requires authentication, specify an XmlResolver with the necessary credentials using the XmlReaderSettings.XmlResolver property.

Security noteSecurity Note

You can use one of the following methods to control which resources the XmlReader can access:

-or-

  • Do not allow the XmlReader to open any external resources by setting the XmlResolver property to null.

The created XmlReader object expands entity references and performs XML normalization of new line characters.

The following example creates a validating XmlReader object that is wrapped around an XmlNodeReader object.

// Create the XmlNodeReader object.
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
XmlNodeReader nodeReader = new XmlNodeReader(doc);

// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add("urn:bookstore-schema", "books.xsd");
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);

// Create a validating reader that wraps the XmlNodeReader object.
XmlReader reader = XmlReader.Create(nodeReader, settings);
// Parse the XML file. 
while (reader.Read());

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft