共用方式為


XmlReader.Create Method (Stream)

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

Creates a new XmlReader instance using the specified stream.

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

Syntax

'Declaration
Public Shared Function Create ( _
    input As Stream _
) As XmlReader
public static XmlReader Create(
    Stream input
)

Parameters

  • input
    Type: System.IO.Stream
    The stream containing 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.

Return Value

Type: System.Xml.XmlReader
An XmlReader object used to read the data contained in the stream.

Exceptions

Exception Condition
NullReferenceException

The input 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.

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.

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.