Freigeben über


XmlReader.Create Method (String, XmlReaderSettings)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates a new instance with the specified URI and XmlReaderSettings.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

Syntax

'Declaration
Public Shared Function Create ( _
    inputUri As String, _
    settings As XmlReaderSettings _
) As XmlReader
public static XmlReader Create(
    string inputUri,
    XmlReaderSettings settings
)

Parameters

  • inputUri
    Type: System.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 nulla null reference (Nothing in Visual Basic), a new XmlXapResolver object is used. For more information, see the Remarks section below.

Return Value

Type: System.Xml.XmlReader
An XmlReader object to read XML data.

Exceptions

Exception Condition
ArgumentNullException

The inputUri value is nulla null reference (Nothing in Visual Basic).

FileNotFoundException

The file specified by the URI cannot be found.

UriFormatException

The URI format is not correct.

Remarks

The inputUri parameter contains the URI to the XML file. This file must be located in the application's XAP resource, unless the XmlResolver has been changed to something other than XmlXapResolver. If you want to download the file from some other location, you must do the following:

  1. Download the data. To do this, initiate an asynchronous request by using HttpWebRequest.

  2. Create an XmlReader by calling one of the Create overloads that take Stream as a parameter and pass the returned Stream that contains the data.

For a code example, see How to: Load an XML File from an Arbitrary URI Location with LINQ to XML.

For information on working with resolvers, see Working with XmlXapResolver, Working with XmlPreloadedResolver.

Examples

The following example uses the Create method.

Dim output As StringBuilder = New StringBuilder()

Dim rs As XmlReaderSettings = New XmlReaderSettings()
rs.DtdProcessing = DtdProcessing.Parse

' XmlXapResolver is the default resolver.
Using reader As XmlReader = XmlReader.Create("nmtoken.xml", rs)
    Dim document As XDocument = XDocument.Load(reader)
    OutputTextBlock.Text = document.ToString()
End Using
StringBuilder output = new StringBuilder();

XmlReaderSettings rs = new XmlReaderSettings();
rs.DtdProcessing = DtdProcessing.Parse;

// XmlXapResolver is the default resolver.
using (XmlReader reader = XmlReader.Create("nmtoken.xml", rs))
{
    XDocument document = XDocument.Load(reader);
    OutputTextBlock.Text = document.ToString();
}

The example uses nmtoken.xml file as input.

<?xml version="1.0"?>
<!DOCTYPE datatype SYSTEM "nmtoken.dtd">
<datatype>
   <desc att="abc">abc</desc>
</datatype>

The xml file references the following nmtoken.dtd file.

<!ELEMENT datatype (desc)>
<!ELEMENT desc (#PCDATA)>
<!ATTLIST desc
   att NMTOKEN #REQUIRED
>

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.