XmlReader.Create Method
![]() |
---|
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.
Assembly: System.Xml (in System.Xml.dll)
Name | Description | |
---|---|---|
![]() ![]() | Create(Stream) | Creates a new XmlReader instance using the specified stream with default settings. |
![]() ![]() | Create(Stream, XmlReaderSettings) | Creates a new XmlReader instance with the specified stream and settings. |
![]() ![]() | Create(Stream, XmlReaderSettings, String) | Creates a new XmlReader instance using the specified stream, base URI, and settings. |
![]() ![]() | Create(Stream, XmlReaderSettings, XmlParserContext) | Creates a new XmlReader instance using the specified stream, settings, and context information for parsing. |
![]() ![]() | Create(String) | Creates a new XmlReader instance with specified URI. |
![]() ![]() | Create(String, XmlReaderSettings) | Creates a new XmlReader instance by using the specified URI and settings. |
![]() ![]() | Create(String, XmlReaderSettings, XmlParserContext) | Creates a new XmlReader instance by using the specified URI, settings, and context information for parsing. |
![]() ![]() | Create(TextReader) | Creates a new XmlReader instance by using the specified text reader. |
![]() ![]() | Create(TextReader, XmlReaderSettings) | Creates a new XmlReader instance by using the specified text 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, XmlParserContext) | Creates a new XmlReader instance by using the specified text reader, settings, and context information for parsing. |
![]() ![]() | Create(XmlReader, XmlReaderSettings) | 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.
![]() |
---|
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 | |
false | |
false | |
false | |
0 | |
0 | |
null | |
An empty XmlSchemaSet object | |
ProcessIdentityConstraints enabled | |
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. | |
Data must be a well-formed XML parsed entity. | |
Data must be validated against a DTD. | DtdProcessing to Parse |
Data must be validated against an XML schema. | ValidationType to Schema |
Data must be validated against an inline XML schema. | ValidationType to Schema |
Type support. | ValidationType to Schema |
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 XmlResolver property to control access to resources:
Set XmlResolver to an XmlSecureResolver object to restrict the resources that the XML reader can access.
-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