XmlReader.Create Method

Definition

Creates a new XmlReader instance.

Overloads

Create(String, XmlReaderSettings, XmlParserContext)

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

Create(TextReader, XmlReaderSettings, XmlParserContext)

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

Create(Stream, XmlReaderSettings, XmlParserContext)

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

Create(Stream, XmlReaderSettings, String)

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

Create(XmlReader, XmlReaderSettings)

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

Create(TextReader, XmlReaderSettings, String)

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

Create(TextReader, XmlReaderSettings)

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

Create(Stream, XmlReaderSettings)

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

Create(String)

Creates a new XmlReader instance with specified URI.

Create(TextReader)

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

Create(Stream)

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

Create(String, XmlReaderSettings)

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

Examples

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 = gcnew XmlReaderSettings;
settings->IgnoreComments = true;
settings->IgnoreProcessingInstructions = true;
settings->IgnoreWhitespace = true;
// 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
// 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 );
// 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)

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

Remarks

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

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:

Setting Default
CheckCharacters true
ConformanceLevel ConformanceLevel.Document
IgnoreComments false
IgnoreProcessingInstructions false
IgnoreWhitespace false
LineNumberOffset 0
LinePositionOffset 0
NameTable null
DtdProcessing Prohibit
Schemas An empty XmlSchemaSet object
ValidationFlags ProcessIdentityConstraints enabled
ValidationType None
XmlResolver A new XmlUrlResolver object. Starting with the .NET Framework 4.5.2, this setting has a default value of null.

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.

Create(String, XmlReaderSettings, XmlParserContext)

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

public:
 static System::Xml::XmlReader ^ Create(System::String ^ inputUri, System::Xml::XmlReaderSettings ^ settings, System::Xml::XmlParserContext ^ inputContext);
public static System.Xml.XmlReader Create (string inputUri, System.Xml.XmlReaderSettings? settings, System.Xml.XmlParserContext? inputContext);
public static System.Xml.XmlReader Create (string inputUri, System.Xml.XmlReaderSettings settings, System.Xml.XmlParserContext inputContext);
static member Create : string * System.Xml.XmlReaderSettings * System.Xml.XmlParserContext -> System.Xml.XmlReader
Public Shared Function Create (inputUri As String, settings As XmlReaderSettings, inputContext As XmlParserContext) As XmlReader

Parameters

inputUri
String

The URI for the file containing the XML data. The XmlResolver object on the XmlReaderSettings object is used to convert the path to a canonical data representation. If XmlResolver is null, a new XmlUrlResolver object is used.

settings
XmlReaderSettings

The settings for the new XmlReader instance. This value can be null.

inputContext
XmlParserContext

The context information required to parse the XML fragment. The context information can include the XmlNameTable to use, encoding, namespace scope, the current xml:lang and xml:space scope, base URI, and document type definition.

This value can be null.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The inputUri value is null.

The XmlReader does not have sufficient permissions to access the location of the XML data.

The NameTable and NameTable properties both contain values. (Only one of these NameTable properties can be set and used).

The file specified by the URI cannot be found.

The URI format is not correct.

Remarks

By default an XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

This means that the XmlReader can access any locations that does not require authentication. If the external resource is located on a network resource that requires authentication, use the XmlReaderSettings.XmlResolver property to specify an XmlResolver with the necessary credentials.

Important

You can restrict the resources that the XmlReader can access by setting the XmlResolver property to an XmlSecureResolver object.

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

Applies to

Create(TextReader, XmlReaderSettings, XmlParserContext)

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

public:
 static System::Xml::XmlReader ^ Create(System::IO::TextReader ^ input, System::Xml::XmlReaderSettings ^ settings, System::Xml::XmlParserContext ^ inputContext);
public static System.Xml.XmlReader Create (System.IO.TextReader input, System.Xml.XmlReaderSettings settings, System.Xml.XmlParserContext inputContext);
public static System.Xml.XmlReader Create (System.IO.TextReader input, System.Xml.XmlReaderSettings? settings, System.Xml.XmlParserContext? inputContext);
static member Create : System.IO.TextReader * System.Xml.XmlReaderSettings * System.Xml.XmlParserContext -> System.Xml.XmlReader
Public Shared Function Create (input As TextReader, settings As XmlReaderSettings, inputContext As XmlParserContext) As XmlReader

Parameters

input
TextReader

The text reader from which to read the XML data. A text reader returns a stream of Unicode characters, so the encoding specified in the XML declaration isn't used by the XML reader to decode the data stream.

settings
XmlReaderSettings

The settings for the new XmlReader instance. This value can be null.

inputContext
XmlParserContext

The context information required to parse the XML fragment. The context information can include the XmlNameTable to use, encoding, namespace scope, the current xml:lang and xml:space scope, base URI, and document type definition.

This value can be null.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The input value is null.

The NameTable and NameTable properties both contain values. (Only one of these NameTable properties can be set and used.)

Examples

The following example creates an XmlReader object that reads an XML fragment.

string xmlFrag ="<item rk:ID='abc-23'>hammer</item> " +
                        "<item rk:ID='r2-435'>paint</item>" +
                        "<item rk:ID='abc-39'>saw</item>";

// Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("rk", "urn:store-items");

// Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

// Create the reader.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader reader = XmlReader.Create(new StringReader(xmlFrag), settings, context);
Dim xmlFrag As String = "<item rk:ID='abc-23'>hammer</item> " & _
                                     "<item rk:ID='r2-435'>paint</item>" & _
                                     "<item rk:ID='abc-39'>saw</item>"

' Create the XmlNamespaceManager.
Dim nt As New NameTable()
Dim nsmgr As New XmlNamespaceManager(nt)
nsmgr.AddNamespace("rk", "urn:store-items")

' Create the XmlParserContext.
Dim context As New XmlParserContext(Nothing, nsmgr, Nothing, XmlSpace.None)

' Create the reader. 
Dim settings As New XmlReaderSettings()
settings.ConformanceLevel = ConformanceLevel.Fragment
Dim reader As XmlReader = XmlReader.Create(New StringReader(xmlFrag), settings, context)

Remarks

By default an XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

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

Important

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.

Applies to

Create(Stream, XmlReaderSettings, XmlParserContext)

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

public:
 static System::Xml::XmlReader ^ Create(System::IO::Stream ^ input, System::Xml::XmlReaderSettings ^ settings, System::Xml::XmlParserContext ^ inputContext);
public static System.Xml.XmlReader Create (System.IO.Stream input, System.Xml.XmlReaderSettings settings, System.Xml.XmlParserContext inputContext);
public static System.Xml.XmlReader Create (System.IO.Stream input, System.Xml.XmlReaderSettings? settings, System.Xml.XmlParserContext? inputContext);
static member Create : System.IO.Stream * System.Xml.XmlReaderSettings * System.Xml.XmlParserContext -> System.Xml.XmlReader
Public Shared Function Create (input As Stream, settings As XmlReaderSettings, inputContext As XmlParserContext) As XmlReader

Parameters

input
Stream

The stream that contains the XML data.

The XmlReader scans the first bytes of the stream looking for a byte order mark or other sign of encoding. When encoding is determined, the encoding is used to continue reading the stream, and processing continues parsing the input as a stream of (Unicode) characters.

settings
XmlReaderSettings

The settings for the new XmlReader instance. This value can be null.

inputContext
XmlParserContext

The context information required to parse the XML fragment. The context information can include the XmlNameTable to use, encoding, namespace scope, the current xml:lang and xml:space scope, base URI, and document type definition.

This value can be null.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The input value is null.

Remarks

By default an XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

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

Important

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.

Applies to

Create(Stream, XmlReaderSettings, String)

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

public:
 static System::Xml::XmlReader ^ Create(System::IO::Stream ^ input, System::Xml::XmlReaderSettings ^ settings, System::String ^ baseUri);
public static System.Xml.XmlReader Create (System.IO.Stream input, System.Xml.XmlReaderSettings? settings, string? baseUri);
public static System.Xml.XmlReader Create (System.IO.Stream input, System.Xml.XmlReaderSettings settings, string baseUri);
static member Create : System.IO.Stream * System.Xml.XmlReaderSettings * string -> System.Xml.XmlReader
Public Shared Function Create (input As Stream, settings As XmlReaderSettings, baseUri As String) As XmlReader

Parameters

input
Stream

The stream that contains the XML data.

The XmlReader scans the first bytes of the stream looking for a byte order mark or other sign of encoding. When encoding is determined, the encoding is used to continue reading the stream, and processing continues parsing the input as a stream of (Unicode) characters.

settings
XmlReaderSettings

The settings for the new XmlReader instance. This value can be null.

baseUri
String

The base URI for the entity or document being read. This value can be null.

Security Note The base URI is used to resolve the relative URI of the XML document. Do not use a base URI from an untrusted source.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The input value is null.

Remarks

By default an XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

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

Important

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.

Applies to

Create(XmlReader, XmlReaderSettings)

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

public:
 static System::Xml::XmlReader ^ Create(System::Xml::XmlReader ^ reader, System::Xml::XmlReaderSettings ^ settings);
public static System.Xml.XmlReader Create (System.Xml.XmlReader reader, System.Xml.XmlReaderSettings settings);
public static System.Xml.XmlReader Create (System.Xml.XmlReader reader, System.Xml.XmlReaderSettings? settings);
static member Create : System.Xml.XmlReader * System.Xml.XmlReaderSettings -> System.Xml.XmlReader
Public Shared Function Create (reader As XmlReader, settings As XmlReaderSettings) As XmlReader

Parameters

reader
XmlReader

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

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

Returns

XmlReader

An object that is wrapped around the specified XmlReader object.

Exceptions

The reader value is null.

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.

Examples

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());
' Create the XmlNodeReader object.
Dim doc As New XmlDocument()
doc.Load("books.xml")
Dim nodeReader As New XmlNodeReader(doc)

' Set the validation settings.
Dim settings As New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas.Add("urn:bookstore-schema", "books.xsd")
AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack

' Create a validating reader that wraps the XmlNodeReader object.
Dim reader As XmlReader = XmlReader.Create(nodeReader, settings)
' Parse the XML file.
While reader.Read()
End While

Remarks

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

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, 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.

Important

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.

Applies to

Create(TextReader, XmlReaderSettings, String)

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

public:
 static System::Xml::XmlReader ^ Create(System::IO::TextReader ^ input, System::Xml::XmlReaderSettings ^ settings, System::String ^ baseUri);
public static System.Xml.XmlReader Create (System.IO.TextReader input, System.Xml.XmlReaderSettings? settings, string? baseUri);
public static System.Xml.XmlReader Create (System.IO.TextReader input, System.Xml.XmlReaderSettings settings, string baseUri);
static member Create : System.IO.TextReader * System.Xml.XmlReaderSettings * string -> System.Xml.XmlReader
Public Shared Function Create (input As TextReader, settings As XmlReaderSettings, baseUri As String) As XmlReader

Parameters

input
TextReader

The text reader from which to read the XML data. A text reader returns a stream of Unicode characters, so the encoding specified in the XML declaration isn't used by the XmlReader to decode the data stream.

settings
XmlReaderSettings

The settings for the new XmlReader instance. This value can be null.

baseUri
String

The base URI for the entity or document being read. This value can be null.

Security Note The base URI is used to resolve the relative URI of the XML document. Do not use a base URI from an untrusted source.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The input value is null.

Remarks

By default an XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

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

Important

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.

Applies to

Create(TextReader, XmlReaderSettings)

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

public:
 static System::Xml::XmlReader ^ Create(System::IO::TextReader ^ input, System::Xml::XmlReaderSettings ^ settings);
public static System.Xml.XmlReader Create (System.IO.TextReader input, System.Xml.XmlReaderSettings settings);
public static System.Xml.XmlReader Create (System.IO.TextReader input, System.Xml.XmlReaderSettings? settings);
static member Create : System.IO.TextReader * System.Xml.XmlReaderSettings -> System.Xml.XmlReader
Public Shared Function Create (input As TextReader, settings As XmlReaderSettings) As XmlReader

Parameters

input
TextReader

The text reader from which to read the XML data. A text reader returns a stream of Unicode characters, so the encoding specified in the XML declaration isn't used by the XML reader to decode the data stream.

settings
XmlReaderSettings

The settings for the new XmlReader. This value can be null.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The input value is null.

Remarks

By default an XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

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

Important

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.

Applies to

Create(Stream, XmlReaderSettings)

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

public:
 static System::Xml::XmlReader ^ Create(System::IO::Stream ^ input, System::Xml::XmlReaderSettings ^ settings);
public static System.Xml.XmlReader Create (System.IO.Stream input, System.Xml.XmlReaderSettings settings);
public static System.Xml.XmlReader Create (System.IO.Stream input, System.Xml.XmlReaderSettings? settings);
static member Create : System.IO.Stream * System.Xml.XmlReaderSettings -> System.Xml.XmlReader
Public Shared Function Create (input As Stream, settings As XmlReaderSettings) As XmlReader

Parameters

input
Stream

The stream that contains the XML data.

The XmlReader scans the first bytes of the stream looking for a byte order mark or other sign of encoding. When encoding is determined, the encoding is used to continue reading the stream, and processing continues parsing the input as a stream of (Unicode) characters.

settings
XmlReaderSettings

The settings for the new XmlReader instance. This value can be null.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The input value is null.

Remarks

By default an XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

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

Important

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.

Applies to

Create(String)

Creates a new XmlReader instance with specified URI.

public:
 static System::Xml::XmlReader ^ Create(System::String ^ inputUri);
public static System.Xml.XmlReader Create (string inputUri);
static member Create : string -> System.Xml.XmlReader
Public Shared Function Create (inputUri As String) As XmlReader

Parameters

inputUri
String

The URI for the file that contains the XML data. The XmlUrlResolver class is used to convert the path to a canonical data representation.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The inputUri value is null.

The XmlReader does not have sufficient permissions to access the location of the XML data.

The file identified by the URI does not exist.

The URI format is not correct.

Note: In .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

Examples

The following example creates an XmlReader object that reads XML data file specified by the URI.

// Create the XmlReader object.
XmlReader reader = XmlReader.Create("books.xml");
' Create the XmlReader object.
Dim reader As XmlReader = XmlReader.Create("books.xml")

Remarks

An XmlReaderSettings object with default settings is used to create the reader. If you wish to specify the features to support on the created reader, use the overload that takes an XmlReaderSettings object as one of its arguments, and pass in an XmlReaderSettings object with the correct settings.

A default XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, 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.

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

Applies to

Create(TextReader)

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

public:
 static System::Xml::XmlReader ^ Create(System::IO::TextReader ^ input);
public static System.Xml.XmlReader Create (System.IO.TextReader input);
static member Create : System.IO.TextReader -> System.Xml.XmlReader
Public Shared Function Create (input As TextReader) As XmlReader

Parameters

input
TextReader

The text reader from which to read the XML data. A text reader returns a stream of Unicode characters, so the encoding specified in the XML declaration is not used by the XML reader to decode the data stream.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The input value is null.

Examples

The following example uses the StringReader class to read an XML string.

string xmlData ="<item productID='124390'>" +
                        "<price>5.95</price>" +
                        "</item>";

// Create the XmlReader object.
XmlReader reader = XmlReader.Create(new StringReader(xmlData));
Dim xmlData As String = "<item productID='124390'>" & _ 
                                     "<price>5.95</price>" & _ 
                                     "</item>"

' Create the XmlReader object.
Dim reader As XmlReader = XmlReader.Create(New StringReader(xmlData))

Remarks

An XmlReaderSettings object with default settings is used to create the reader. If you wish to specify the features to support on the created reader, use the overload that takes an XmlReaderSettings object as one of its arguments, and pass in an XmlReaderSettings object with the correct settings.

A default XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, 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.

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

Applies to

Create(Stream)

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

public:
 static System::Xml::XmlReader ^ Create(System::IO::Stream ^ input);
public static System.Xml.XmlReader Create (System.IO.Stream input);
static member Create : System.IO.Stream -> System.Xml.XmlReader
Public Shared Function Create (input As Stream) As XmlReader

Parameters

input
Stream

The stream that contains the XML data.

The XmlReader scans the first bytes of the stream looking for a byte order mark or other sign of encoding. When encoding is determined, the encoding is used to continue reading the stream, and processing continues parsing the input as a stream of (Unicode) characters.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The input value is null.

The XmlReader does not have sufficient permissions to access the location of the XML data.

Examples

The following example creates an XmlReader object that reads from a FileStream.


FileStream fs = new FileStream(@"C:\data\books.xml", FileMode.OpenOrCreate,
                                                                    FileAccess.Read, FileShare.Read);

// Create the XmlReader object.
XmlReader reader = XmlReader.Create(fs);
    Dim fs As New FileStream("C:\data\books.xml", FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
    
    ' Create the XmlReader object.
    Dim reader As XmlReader = XmlReader.Create(fs)

End Sub

Remarks

An XmlReaderSettings object with default settings is used to create the reader. If you wish to specify the features to support on the created reader, use the overload that takes an XmlReaderSettings object as one of its arguments, and pass in an XmlReaderSettings object with the correct settings.

A default XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, 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.

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

Applies to

Create(String, XmlReaderSettings)

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

public:
 static System::Xml::XmlReader ^ Create(System::String ^ inputUri, System::Xml::XmlReaderSettings ^ settings);
public static System.Xml.XmlReader Create (string inputUri, System.Xml.XmlReaderSettings settings);
public static System.Xml.XmlReader Create (string inputUri, System.Xml.XmlReaderSettings? settings);
static member Create : string * System.Xml.XmlReaderSettings -> System.Xml.XmlReader
Public Shared Function Create (inputUri As String, settings As XmlReaderSettings) As XmlReader

Parameters

inputUri
String

The URI for the file containing the XML data. The XmlResolver object on the XmlReaderSettings object is used to convert the path to a canonical data representation. If XmlResolver is null, a new XmlUrlResolver object is used.

settings
XmlReaderSettings

The settings for the new XmlReader instance. This value can be null.

Returns

XmlReader

An object that is used to read the XML data in the stream.

Exceptions

The inputUri value is null.

The file specified by the URI cannot be found.

The URI format is not correct.

Note: In .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

Examples

The following example creates an XmlReader object that supports document type definition (DTD) validation.

// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
settings.ValidationType = ValidationType.DTD;
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);

 // Create the XmlReader object.
XmlReader reader = XmlReader.Create("itemDTD.xml", settings);

// Parse the file.
while (reader.Read()) {}
' Set the validation settings.
Dim settings As New XmlReaderSettings()
settings.DtdProcessing = DtdProcessing.Parse
settings.ValidationType = ValidationType.DTD
AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack

' Create the XmlReader object.
Dim reader As XmlReader = XmlReader.Create("itemDTD.xml", settings)

' Parse the file. 
While reader.Read()
End While

Remarks

By default an XmlUrlResolver with no credentials is used to access any external resources such as a document type definition (DTD), entities, schemas, and so on.

Important

Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

This means that the XmlReader can access any locations that does not require authentication. If the external resource is located on a network resource that requires authentication, use the XmlReaderSettings.XmlResolver property to specify an XmlResolver with the necessary credentials.

Important

You can restrict the resources that the XmlReader can access by setting the XmlResolver property to an XmlSecureResolver object.

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

Applies to