XmlReader.Create XmlReader.Create XmlReader.Create XmlReader.Create Method

定义

创建一个新的 XmlReader 实例。Creates a new XmlReader instance.

重载

Create(String, XmlReaderSettings, XmlParserContext) Create(String, XmlReaderSettings, XmlParserContext) Create(String, XmlReaderSettings, XmlParserContext) Create(String, XmlReaderSettings, XmlParserContext)

使用指定的 URI、设置和进行分析的上下文信息创建一个新的 XmlReader 实例。Creates a new XmlReader instance by using the specified URI, settings, and context information for parsing.

Create(TextReader, XmlReaderSettings, XmlParserContext) Create(TextReader, XmlReaderSettings, XmlParserContext) Create(TextReader, XmlReaderSettings, XmlParserContext) Create(TextReader, XmlReaderSettings, XmlParserContext)

使用指定的文本读取器、设置和要用于分析的上下文信息创建一个新的 XmlReader 实例。Creates a new XmlReader instance by using the specified text reader, settings, and context information for parsing.

Create(Stream, XmlReaderSettings, XmlParserContext) Create(Stream, XmlReaderSettings, XmlParserContext) Create(Stream, XmlReaderSettings, XmlParserContext) Create(Stream, XmlReaderSettings, XmlParserContext)

使用指定的流、设置和用于分析的上下文信息创建一个新的 XmlReader 实例。Creates a new XmlReader instance using the specified stream, settings, and context information for parsing.

Create(Stream, XmlReaderSettings, String) Create(Stream, XmlReaderSettings, String) Create(Stream, XmlReaderSettings, String) Create(Stream, XmlReaderSettings, String)

使用指定的流、基 URI 和设置创建一个新的 XmlReader 实例。Creates a new XmlReader instance using the specified stream, base URI, and settings.

Create(XmlReader, XmlReaderSettings) Create(XmlReader, XmlReaderSettings) Create(XmlReader, XmlReaderSettings) Create(XmlReader, XmlReaderSettings)

使用指定的 XML 读取器和设置创建一个新的 XmlReader 实例。Creates a new XmlReader instance by using the specified XML reader and settings.

Create(TextReader, XmlReaderSettings, String) Create(TextReader, XmlReaderSettings, String) Create(TextReader, XmlReaderSettings, String) Create(TextReader, XmlReaderSettings, String)

使用指定的文本读取器、设置和基 URI 创建一个新的 XmlReaderCreates a new XmlReader instance by using the specified text reader, settings, and base URI.

Create(TextReader, XmlReaderSettings) Create(TextReader, XmlReaderSettings) Create(TextReader, XmlReaderSettings) Create(TextReader, XmlReaderSettings)

使用指定的文本读取器和设置创建一个新的 XmlReader 实例。Creates a new XmlReader instance by using the specified text reader and settings.

Create(Stream, XmlReaderSettings) Create(Stream, XmlReaderSettings) Create(Stream, XmlReaderSettings) Create(Stream, XmlReaderSettings)

使用指定的流和设置创建一个新的 XmlReader 实例。Creates a new XmlReader instance with the specified stream and settings.

Create(String) Create(String) Create(String) Create(String)

用指定的 URI 创建一个新的 XmlReader 实例。Creates a new XmlReader instance with specified URI.

Create(TextReader) Create(TextReader) Create(TextReader) Create(TextReader)

使用指定的文本读取器创建一个新的 XmlReader 实例。Creates a new XmlReader instance by using the specified text reader.

Create(Stream) Create(Stream) Create(Stream) Create(Stream)

使用带默认设置的指定流创建新的 XmlReader 实例。Creates a new XmlReader instance using the specified stream with default settings.

Create(String, XmlReaderSettings) Create(String, XmlReaderSettings) Create(String, XmlReaderSettings) Create(String, XmlReaderSettings)

使用指定的 URI 和设置创建新的 XmlReader 实例。Creates a new XmlReader instance by using the specified URI and settings.

示例

此示例将创建一个 XML 读取器,该读取器可去除无意义的空白、去除注释并执行片段级别的一致性检查。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)

下面的示例使用XmlUrlResolver具有默认凭据的来访问文件。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)

此示例将读取器链接到添加 DTD 和 XML 架构验证。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

注解

大多数重载都包含一个settings接受XmlReaderSettings对象的参数。 CreateMost of the Create overloads include a settings parameter that accepts an XmlReaderSettings object. 您可以使用此对象执行以下操作:You can use this object to:

  • 指定要在XmlReader对象上支持的功能。Specify which features you want supported on the XmlReader object.

  • 重新使用XmlReaderSettings对象以创建多个读取器。Reuse the XmlReaderSettings object to create multiple readers. 可以使用相同的设置创建多个具有相同功能的读取器。You can use the same settings to create multiple readers with the same functionality. 或者,您可以修改XmlReaderSettings实例上的设置,并使用一组不同的功能创建新的读取器。Or, you can modify the settings on an XmlReaderSettings instance and create a new reader with a different set of features.

  • 向现有的 XML 读取器添加功能。Add features to an existing XML reader. Create 方法可以接受其他 XmlReader 对象。The Create method can accept another XmlReader object. 基础XmlReader对象可以是用户定义的读取器XmlTextReader 、对象或要向其添加XmlReader附加功能的另一个实例。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.

  • 充分利用功能,如更好的一致性检查和符合XML 1.0 (第四版)建议,这些建议仅在静态XmlReader Create方法创建的对象上可用。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.

备注

尽管 .NET Framework XmlReader包括类的具体实现,例如XmlTextReaderXmlNodeReaderXmlValidatingReader Create类,但我们建议你XmlReader使用付款方式.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

如果使用CreateXmlReaderSettings接受对象的重载,则使用以下默认读取器设置: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 00
LinePositionOffset 00
NameTable null
DtdProcessing Prohibit
Schemas 空的 XmlSchemaSet 对象An empty XmlSchemaSet object
ValidationFlags ProcessIdentityConstraints能够ProcessIdentityConstraints enabled
ValidationType None
XmlResolver 一个新 XmlUrlResolver 对象。A new XmlUrlResolver object. 从 .NET Framework 4.5.2 开始,此设置的默认值为nullStarting with the .NET Framework 4.5.2, this setting has a default value of null.

常见方案的设置Settings for common scenarios

下面是XmlReaderSettings应该为一些典型的 XML 读取器方案设置的属性。Here are the XmlReaderSettings properties you should set for some of the typical XML reader scenarios.

需求Requirement SetSet
数据必须是格式正确的 XML 文档。Data must be a well-formed XML document. ConformanceLevelDocumentConformanceLevel to Document.
数据必须是格式正确的 XML 分析实体。Data must be a well-formed XML parsed entity. ConformanceLevelFragmentConformanceLevel to Fragment.
数据必须针对 DTD 进行验证。Data must be validated against a DTD. DtdProcessingParseDtdProcessing to Parse
ValidationTypeDTDValidationType to DTD.
必须根据 XML 架构对数据进行验证。Data must be validated against an XML schema. ValidationTypeSchemaValidationType to Schema
Schemas要用于XmlSchemaSet验证的。Schemas to the XmlSchemaSet to use for validation. 请注意XmlReader ,不支持 XML 数据缩减(XDR)架构验证。Note that XmlReader doesn't support XML-Data Reduced (XDR) schema validation.
必须根据内联 XML 架构对数据进行验证。Data must be validated against an inline XML schema. ValidationTypeSchemaValidationType to Schema
ValidationFlagsProcessInlineSchemaValidationFlags to ProcessInlineSchema.
类型支持。Type support. ValidationTypeSchemaValidationType to Schema
Schemas要使用XmlSchemaSet的。Schemas to the XmlSchemaSet to use.

XmlReader不支持 XML 数据缩减(XDR)架构验证。XmlReader doesn't support XML-Data Reduced (XDR) schema validation.

异步编程Asynchronous programming

在同步模式下, Create方法从文件、流或文本读取器的缓冲区中读取第一个数据块。In synchronous mode, the Create method reads the first chunk of data from the buffer of the file, stream, or text reader. 如果 i/o 操作失败,这可能会引发异常。This may throw an exception if an I/O operation fails. 在异步模式下,第一次 i/o 操作的执行是读取操作,因此,发生读取操作时会引发发生的异常。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

默认情况下, XmlReader XmlUrlResolver使用没有用户凭据的对象打开资源。By default, the XmlReader uses an XmlUrlResolver object with no user credentials to open resources. 这意味着,默认情况下,XML 读取器可以访问不需要凭据的任何位置。This means that, by default, the XML reader can access any location that doesn't require credentials. XmlResolver使用属性来控制对资源的访问:Use the XmlResolver property to control access to resources:

- 或 --or-

  • 设置XmlResolvernull ,以防止 XML 读取器打开任何外部资源。Set XmlResolver to null to prevent the XML reader from opening any external resources.

Create(String, XmlReaderSettings, XmlParserContext) Create(String, XmlReaderSettings, XmlParserContext) Create(String, XmlReaderSettings, XmlParserContext) Create(String, XmlReaderSettings, XmlParserContext)

使用指定的 URI、设置和进行分析的上下文信息创建一个新的 XmlReader 实例。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);
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

参数

inputUri
String String String String

包含 XML 数据的文件的 URI。The URI for the file containing the XML data. XmlReaderSettings 对象上的 XmlResolver 对象用于将路径转换为规范化数据表示形式。The XmlResolver object on the XmlReaderSettings object is used to convert the path to a canonical data representation. 如果 XmlResolvernull,则使用新的 XmlUrlResolver 对象。If XmlResolver is null, a new XmlUrlResolver object is used.

settings
XmlReaderSettings XmlReaderSettings XmlReaderSettings XmlReaderSettings

XmlReader 实例的设置。The settings for the new XmlReader instance. 此值可为 nullThis value can be null.

inputContext
XmlParserContext XmlParserContext XmlParserContext XmlParserContext

分析 XML 片段所需的上下文信息.The context information required to parse the XML fragment. 上下文信息可以包括要使用的 XmlNameTable、编码、命名空间范围、当前的 xml:langxml:space 范围、基 URI 和文档类型定义。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.

此值可为 nullThis value can be null.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

inputUri 值为 nullThe inputUri value is null.

XmlReader 没有足够的权限访问 XML 数据的位置。The XmlReader does not have sufficient permissions to access the location of the XML data.

NameTableNameTable 属性都包含值。The NameTable and NameTable properties both contain values. (只能设置并使用这些 NameTable 属性之中的一个)。(Only one of these NameTable properties can be set and used).

找不到 URI 指定的文件。The file specified by the URI cannot be found.

URI 格式不正确。The URI format is not correct.

注解

默认情况下XmlUrlResolver ,不带凭据的将用于访问任何外部资源,例如文档类型定义(DTD)、实体、架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

这意味着XmlReader可以访问不需要身份验证的任何位置。This means that the XmlReader can access any locations that does not require authentication. 如果外部资源位于要求身份验证的网络资源上,请使用XmlReaderSettings.XmlResolver属性XmlResolver指定具有必要凭据的。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.

重要

可以通过XmlReader XmlResolver将属性设置为XmlSecureResolver对象来限制可以访问的资源。You can restrict the resources that the XmlReader can access by setting the XmlResolver property to an XmlSecureResolver object.

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(TextReader, XmlReaderSettings, XmlParserContext) Create(TextReader, XmlReaderSettings, XmlParserContext) Create(TextReader, XmlReaderSettings, XmlParserContext) Create(TextReader, XmlReaderSettings, XmlParserContext)

使用指定的文本读取器、设置和要用于分析的上下文信息创建一个新的 XmlReader 实例。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);
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

参数

input
TextReader TextReader TextReader TextReader

从其中读取 XML 数据的文本读取器。The text reader from which to read the XML data. 由于文本读取器返回的是 Unicode 字符流,因此,XML 读取器未使用 XML 声明中指定的编码对数据流进行解码。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 XmlReaderSettings XmlReaderSettings XmlReaderSettings

XmlReader 实例的设置。The settings for the new XmlReader instance. 此值可为 nullThis value can be null.

inputContext
XmlParserContext XmlParserContext XmlParserContext XmlParserContext

分析 XML 片段所需的上下文信息.The context information required to parse the XML fragment. 上下文信息可以包括要使用的 XmlNameTable、编码、命名空间范围、当前的 xml:langxml:space 范围、基 URI 和文档类型定义。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.

此值可为 nullThis value can be null.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

NameTableNameTable 属性都包含值。The NameTable and NameTable properties both contain values. (只能设置并使用这些 NameTable 属性之中的一个)。(Only one of these NameTable properties can be set and used).

示例

下面的示例创建一个XmlReader读取 XML 片段的对象。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)

注解

默认情况下XmlUrlResolver ,不带凭据的将用于访问任何外部资源,例如文档类型定义(DTD)、实体、架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

如果外部资源位于要求身份验证的网络资源上,请使用XmlReaderSettings.XmlResolver属性XmlResolver指定具有必要凭据的。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.

重要

你可以使用以下方法之一来控制XmlReader可访问的资源:You can use one of the following methods to control which resources the XmlReader can access:

- 或 --or-

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(Stream, XmlReaderSettings, XmlParserContext) Create(Stream, XmlReaderSettings, XmlParserContext) Create(Stream, XmlReaderSettings, XmlParserContext) Create(Stream, XmlReaderSettings, XmlParserContext)

使用指定的流、设置和用于分析的上下文信息创建一个新的 XmlReader 实例。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);
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

参数

input
Stream Stream Stream Stream

包含 XML 数据的流。The stream that contains the XML data.

XmlReader 对流的前几个字节进行扫描,查找字节顺序标记或其他编码标志。The XmlReader scans the first bytes of the stream looking for a byte order mark or other sign of encoding. 在确定编码方式后,使用该编码方式继续读取流,而处理过程继续将输入内容分析为 (Unicode) 字符流。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 XmlReaderSettings XmlReaderSettings XmlReaderSettings

XmlReader 实例的设置。The settings for the new XmlReader instance. 此值可为 nullThis value can be null.

inputContext
XmlParserContext XmlParserContext XmlParserContext XmlParserContext

分析 XML 片段所需的上下文信息.The context information required to parse the XML fragment. 上下文信息可以包括要使用的 XmlNameTable、编码、命名空间范围、当前的 xml:langxml:space 范围、基 URI 和文档类型定义。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.

此值可为 nullThis value can be null.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

注解

默认情况下XmlUrlResolver ,不带凭据的将用于访问任何外部资源,例如文档类型定义(DTD)、实体、架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

如果外部资源位于要求身份验证的网络资源上,请使用XmlReaderSettings.XmlResolver属性XmlResolver指定具有必要凭据的。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.

重要

你可以使用以下方法之一来控制XmlReader可访问的资源:You can use one of the following methods to control which resources the XmlReader can access:

-or-

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(Stream, XmlReaderSettings, String) Create(Stream, XmlReaderSettings, String) Create(Stream, XmlReaderSettings, String) Create(Stream, XmlReaderSettings, String)

使用指定的流、基 URI 和设置创建一个新的 XmlReader 实例。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);
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

参数

input
Stream Stream Stream Stream

包含 XML 数据的流。The stream that contains the XML data.

XmlReader 对流的前几个字节进行扫描,查找字节顺序标记或其他编码标志。The XmlReader scans the first bytes of the stream looking for a byte order mark or other sign of encoding. 在确定编码方式后,使用该编码方式继续读取流,而处理过程继续将输入内容分析为 (Unicode) 字符流。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 XmlReaderSettings XmlReaderSettings XmlReaderSettings

XmlReader 实例的设置。The settings for the new XmlReader instance. 此值可为 nullThis value can be null.

baseUri
String String String String

正在读取的实体或文档的基 URI。The base URI for the entity or document being read. 此值可为 nullThis value can be null.

安全说明 基 URI 用于解析 XML 文档的相对 URI。Security Note The base URI is used to resolve the relative URI of the XML document. 不要使用来自非信任源的基 URI。Do not use a base URI from an untrusted source.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

注解

默认情况下XmlUrlResolver ,不带凭据的将用于访问任何外部资源,例如文档类型定义(DTD)、实体、架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

如果外部资源位于要求身份验证的网络资源上,请使用XmlReaderSettings.XmlResolver属性XmlResolver指定具有必要凭据的。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.

重要

你可以使用以下方法之一来控制XmlReader可访问的资源:You can use one of the following methods to control which resources the XmlReader can access:

- 或 --or-

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(XmlReader, XmlReaderSettings) Create(XmlReader, XmlReaderSettings) Create(XmlReader, XmlReaderSettings) Create(XmlReader, XmlReaderSettings)

使用指定的 XML 读取器和设置创建一个新的 XmlReader 实例。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);
static member Create : System.Xml.XmlReader * System.Xml.XmlReaderSettings -> System.Xml.XmlReader
Public Shared Function Create (reader As XmlReader, settings As XmlReaderSettings) As XmlReader

参数

reader
XmlReader XmlReader XmlReader XmlReader

要用作基础 XML 读取器的对象。The object that you want to use as the underlying XML reader.

settings
XmlReaderSettings XmlReaderSettings XmlReaderSettings XmlReaderSettings

XmlReader 实例的设置。The settings for the new XmlReader instance.

XmlReaderSettings 对象的一致性级别要么必须与基础读取器的一致性级别匹配,要么必须设置为 AutoThe conformance level of the XmlReaderSettings object must either match the conformance level of the underlying reader, or it must be set to Auto.

返回

在指定的 XmlReader 对象周围包装的对象。An object that is wrapped around the specified XmlReader object.

异常

XmlReaderSettings 对象指定的一致性级别与基础读取器的一致性级别不一致。If the XmlReaderSettings object specifies a conformance level that is not consistent with conformance level of the underlying reader.

-or- 基础 XmlReader 处于 ErrorClosed 状态。The underlying XmlReader is in an Error or Closed state.

示例

下面的示例创建一个在XmlReader XmlNodeReader对象周围环绕的验证对象。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

注解

此方法允许您将其他功能添加到基础XmlReader对象。This method allows you add additional features to an underlying XmlReader object. 基础XmlReader对象可以是由Create方法XmlReader创建的另一个对象,也XmlReader可以是使用一个具体XmlReader实现创建的对象。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.

不带XmlUrlResolver凭据的默认值用于访问任何外部资源(如架构)。A default XmlUrlResolver with no credentials is used to access any external resources such as a schema.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

如果外部资源位于要求身份验证的网络资源上,则XmlResolver XmlReaderSettings.XmlResolver使用属性指定具有所需凭据的。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.

重要

你可以使用以下方法之一来控制XmlReader可访问的资源:You can use one of the following methods to control which resources the XmlReader can access:

-or-

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(TextReader, XmlReaderSettings, String) Create(TextReader, XmlReaderSettings, String) Create(TextReader, XmlReaderSettings, String) Create(TextReader, XmlReaderSettings, String)

使用指定的文本读取器、设置和基 URI 创建一个新的 XmlReaderCreates 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);
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

参数

input
TextReader TextReader TextReader TextReader

从其中读取 XML 数据的文本读取器。The text reader from which to read the XML data. 由于文本读取器返回的是 Unicode 字符流,因此,XmlReader 未使用 XML 声明中指定的编码对数据流进行解码。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 XmlReaderSettings XmlReaderSettings XmlReaderSettings

XmlReader 实例的设置。The settings for the new XmlReader instance. 此值可为 nullThis value can be null.

baseUri
String String String String

正在读取的实体或文档的基 URI。The base URI for the entity or document being read. 此值可为 nullThis value can be null.

安全说明 基 URI 用于解析 XML 文档的相对 URI。Security Note The base URI is used to resolve the relative URI of the XML document. 不要使用来自非信任源的基 URI。Do not use a base URI from an untrusted source.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

注解

默认情况下XmlUrlResolver ,不带凭据的将用于访问任何外部资源,例如文档类型定义(DTD)、实体、架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

如果外部资源位于要求身份验证的网络资源上,请使用XmlReaderSettings.XmlResolver属性XmlResolver指定具有必要凭据的。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.

重要

你可以使用以下方法之一来控制XmlReader可访问的资源:You can use one of the following methods to control which resources the XmlReader can access:

-or-

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(TextReader, XmlReaderSettings) Create(TextReader, XmlReaderSettings) Create(TextReader, XmlReaderSettings) Create(TextReader, XmlReaderSettings)

使用指定的文本读取器和设置创建一个新的 XmlReader 实例。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);
static member Create : System.IO.TextReader * System.Xml.XmlReaderSettings -> System.Xml.XmlReader
Public Shared Function Create (input As TextReader, settings As XmlReaderSettings) As XmlReader

参数

input
TextReader TextReader TextReader TextReader

从其中读取 XML 数据的文本读取器。The text reader from which to read the XML data. 由于文本读取器返回的是 Unicode 字符流,因此,XML 读取器未使用 XML 声明中指定的编码对数据流进行解码。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 XmlReaderSettings XmlReaderSettings XmlReaderSettings

XmlReader 的设置。The settings for the new XmlReader. 此值可为 nullThis value can be null.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

注解

默认情况下XmlUrlResolver ,不带凭据的将用于访问任何外部资源,例如文档类型定义(DTD)、实体、架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

如果外部资源位于要求身份验证的网络资源上,请使用XmlReaderSettings.XmlResolver属性XmlResolver指定具有必要凭据的。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.

重要

你可以使用以下方法之一来控制XmlReader可访问的资源:You can use one of the following methods to control which resources the XmlReader can access:

- 或 --or-

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(Stream, XmlReaderSettings) Create(Stream, XmlReaderSettings) Create(Stream, XmlReaderSettings) Create(Stream, XmlReaderSettings)

使用指定的流和设置创建一个新的 XmlReader 实例。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);
static member Create : System.IO.Stream * System.Xml.XmlReaderSettings -> System.Xml.XmlReader
Public Shared Function Create (input As Stream, settings As XmlReaderSettings) As XmlReader

参数

input
Stream Stream Stream Stream

包含 XML 数据的流。The stream that contains the XML data.

XmlReader 对流的前几个字节进行扫描,查找字节顺序标记或其他编码标志。The XmlReader scans the first bytes of the stream looking for a byte order mark or other sign of encoding. 在确定编码方式后,使用该编码方式继续读取流,而处理过程继续将输入内容分析为 (Unicode) 字符流。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 XmlReaderSettings XmlReaderSettings XmlReaderSettings

XmlReader 实例的设置。The settings for the new XmlReader instance. 此值可为 nullThis value can be null.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

注解

默认情况下XmlUrlResolver ,不带凭据的将用于访问任何外部资源,例如文档类型定义(DTD)、实体、架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

如果外部资源位于要求身份验证的网络资源上,请使用XmlReaderSettings.XmlResolver属性XmlResolver指定具有必要凭据的。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.

重要

你可以使用以下方法之一来控制XmlReader可访问的资源:You can use one of the following methods to control which resources the XmlReader can access:

-or-

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(String) Create(String) Create(String) Create(String)

用指定的 URI 创建一个新的 XmlReader 实例。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

参数

inputUri
String String String String

包含 XML 数据的文件的 URI。The URI for the file that contains the XML data. XmlUrlResolver 类用于将路径转换为规范化数据表示形式。The XmlUrlResolver class is used to convert the path to a canonical data representation.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

inputUri 值为 nullThe inputUri value is null.

XmlReader 没有足够的权限访问 XML 数据的位置。The XmlReader does not have sufficient permissions to access the location of the XML data.

由 URI 标识的文件不存在。The file identified by the URI does not exist.

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 FormatExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

URI 格式不正确。The URI format is not correct.

示例

下面的示例创建一个XmlReader对象,该对象读取 URI 指定的 XML 数据文件。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")

注解

使用具有默认设置的对象来创建读取器。XmlReaderSettingsAn XmlReaderSettings object with default settings is used to create the reader. 如果希望在创建的读取器上指定支持的功能,请使用将XmlReaderSettings对象作为其参数之一的重载,并传入XmlReaderSettings具有正确设置的对象。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.

不带XmlUrlResolver凭据的默认值用于访问任何外部资源,例如文档类型定义(DTD)、实体和架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

如果外部资源位于要求身份验证的网络资源上,则XmlResolver XmlReaderSettings.XmlResolver使用属性指定具有所需凭据的。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.

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(TextReader) Create(TextReader) Create(TextReader) Create(TextReader)

使用指定的文本读取器创建一个新的 XmlReader 实例。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

参数

input
TextReader TextReader TextReader TextReader

从其中读取 XML 数据的文本读取器。The text reader from which to read the XML data. 由于文本读取器返回的是 Unicode 字符流,因此,XML 读取器未使用 XML 声明中指定的编码对数据流进行解码。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.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

示例

下面的示例使用StringReader类读取 XML 字符串。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))

注解

使用具有默认设置的对象来创建读取器。XmlReaderSettingsAn XmlReaderSettings object with default settings is used to create the reader. 如果希望在创建的读取器上指定支持的功能,请使用将XmlReaderSettings对象作为其参数之一的重载,并传入XmlReaderSettings具有正确设置的对象。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.

不带XmlUrlResolver凭据的默认值用于访问任何外部资源,例如文档类型定义(DTD)、实体和架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

如果外部资源位于要求身份验证的网络资源上,则XmlResolver XmlReaderSettings.XmlResolver使用属性指定具有所需凭据的。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.

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(Stream) Create(Stream) Create(Stream) Create(Stream)

使用带默认设置的指定流创建新的 XmlReader 实例。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

参数

input
Stream Stream Stream Stream

包含 XML 数据的流。The stream that contains the XML data.

XmlReader 对流的前几个字节进行扫描,查找字节顺序标记或其他编码标志。The XmlReader scans the first bytes of the stream looking for a byte order mark or other sign of encoding. 在确定编码方式后,使用该编码方式继续读取流,而处理过程继续将输入内容分析为 (Unicode) 字符流。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.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

XmlReader 没有足够的权限访问 XML 数据的位置。The XmlReader does not have sufficient permissions to access the location of the XML data.

示例

下面的示例创建一个XmlReader FileStream从中读取的对象。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 'FileStream

注解

使用具有默认设置的对象来创建读取器。XmlReaderSettingsAn XmlReaderSettings object with default settings is used to create the reader. 如果希望在创建的读取器上指定支持的功能,请使用将XmlReaderSettings对象作为其参数之一的重载,并传入XmlReaderSettings具有正确设置的对象。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.

不带XmlUrlResolver凭据的默认值用于访问任何外部资源,例如文档类型定义(DTD)、实体和架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

如果外部资源位于要求身份验证的网络资源上,则XmlResolver XmlReaderSettings.XmlResolver使用属性指定具有所需凭据的。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.

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

Create(String, XmlReaderSettings) Create(String, XmlReaderSettings) Create(String, XmlReaderSettings) Create(String, XmlReaderSettings)

使用指定的 URI 和设置创建新的 XmlReader 实例。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);
static member Create : string * System.Xml.XmlReaderSettings -> System.Xml.XmlReader
Public Shared Function Create (inputUri As String, settings As XmlReaderSettings) As XmlReader

参数

inputUri
String String String String

包含 XML 数据的文件的 URI。The URI for the file containing the XML data. XmlReaderSettings 对象上的 XmlResolver 对象用于将路径转换为规范化数据表示形式。The XmlResolver object on the XmlReaderSettings object is used to convert the path to a canonical data representation. 如果 XmlResolvernull,则使用新的 XmlUrlResolver 对象。If XmlResolver is null, a new XmlUrlResolver object is used.

settings
XmlReaderSettings XmlReaderSettings XmlReaderSettings XmlReaderSettings

XmlReader 实例的设置。The settings for the new XmlReader instance. 此值可为 nullThis value can be null.

返回

一个用于读取数据流中所含数据的对象。An object that is used to read the XML data in the stream.

异常

inputUri 值为 nullThe inputUri value is null.

找不到 URI 指定的文件。The file specified by the URI cannot be found.

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 FormatExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

URI 格式不正确。The URI format is not correct.

示例

下面的示例创建一个XmlReader支持文档类型定义(DTD)验证的对象。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

注解

默认情况下XmlUrlResolver ,不带凭据的将用于访问任何外部资源,例如文档类型定义(DTD)、实体、架构等。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.

重要

从 .NET Framework 4.5.2 开始,不提供默认XmlUrlResolver值。Starting with the .NET Framework 4.5.2, no default XmlUrlResolver is provided. 如果你的解决方案面向 .NET Framework 4.5.2 或更高版本,请XmlResolver XmlReaderSettings.XmlResolver使用属性指定。If your solution targets the .NET Framework 4.5.2 or later versions, specify an XmlResolver using the XmlReaderSettings.XmlResolver property.

这意味着XmlReader可以访问不需要身份验证的任何位置。This means that the XmlReader can access any locations that does not require authentication. 如果外部资源位于要求身份验证的网络资源上,请使用XmlReaderSettings.XmlResolver属性XmlResolver指定具有必要凭据的。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.

重要

可以通过XmlReader XmlResolver将属性设置为XmlSecureResolver对象来限制可以访问的资源。You can restrict the resources that the XmlReader can access by setting the XmlResolver property to an XmlSecureResolver object.

创建XmlReader的对象扩展实体引用并执行新行字符的 XML 规范化。The created XmlReader object expands entity references and performs XML normalization of new line characters.

适用于