共用方式為


XmlReader.Create Method (String)

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

Creates a new XmlReader instance with specified URI.

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

Syntax

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

Parameters

  • inputUri
    Type: System.String
    The URI for the file containing the XML data. The XmlXapResolver class is used to convert the path to a canonical data representation. For more information, see the Remarks section below.

Return Value

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

Exceptions

Exception Condition
NullReferenceException

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

SecurityException

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

FileNotFoundException

The file identified by the URI does not exist.

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.

An XmlReaderSettings object with default settings is used to create the reader. If you want 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.

Examples

The following example uses the Create method.

Dim output As StringBuilder = New StringBuilder()

' XmlXapResolver is the default resolver. 
Using reader As XmlReader = XmlReader.Create("book.xml")

    ' Moves the reader to the root element.
    reader.MoveToContent()

    reader.ReadToFollowing("book")
    ' Note that ReadInnerXml only returns the markup of the node's children
    ' so the book's attributes are not returned.
    output.AppendLine("Read the first book using ReadInnerXml...")
    output.AppendLine(reader.ReadInnerXml())

    reader.ReadToFollowing("book")

    ' ReadOuterXml returns the markup for the current node and its children
    ' so the book's attributes are also returned.
    output.AppendLine("Read the second book using ReadOuterXml...")
    output.AppendLine(reader.ReadOuterXml())
End Using

OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();

// XmlXapResolver is the default resolver.
using (XmlReader reader = XmlReader.Create("book.xml"))
{
    // Moves the reader to the root element.
    reader.MoveToContent();

    reader.ReadToFollowing("book");
    // Note that ReadInnerXml only returns the markup of the node's children
    // so the book's attributes are not returned.
    output.AppendLine("Read the first book using ReadInnerXml...");
    output.AppendLine(reader.ReadInnerXml());

    reader.ReadToFollowing("book");

    // ReadOuterXml returns the markup for the current node and its children
    // so the book's attributes are also returned.
    output.AppendLine("Read the second book using ReadOuterXml...");
    output.AppendLine(reader.ReadOuterXml());

}

OutputTextBlock.Text = output.ToString();

The example uses bool.xml file as input.

<bookstore>
    <book genre='novel' ISBN='10-861003-324'>
        <title>The Handmaid's Tale</title>
        <price>19.95</price>
    </book>
    <book genre='novel' ISBN='1-861001-57-5'>
        <title>Pride And Prejudice</title>
        <price>24.95</price>
    </book>
</bookstore>

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.