XmlTextReader Constructor (String, XmlNodeType, XmlParserContext)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Initializes a new instance of the XmlTextReader class with the specified string, XmlNodeType, and XmlParserContext.

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

Public Sub New (
	xmlFragment As String,
	fragType As XmlNodeType,
	context As XmlParserContext
)

Parameters

xmlFragment
Type: System.String

The string containing the XML fragment to parse.

fragType
Type: System.Xml.XmlNodeType

The XmlNodeType of the XML fragment. This also determines what the fragment string can contain. (See table below.)

context
Type: System.Xml.XmlParserContext

The XmlParserContext in which the xmlFragment is to be parsed. This includes the XmlNameTable to use, encoding, namespace scope, the current xml:lang, and the xml:space scope.

Exception Condition
XmlException

fragType is not an Element, Attribute, or DocumentXmlNodeType.

ArgumentNullException

xmlFragment is null.

System_CAPS_noteNote

Starting with the .NET Framework 2.0, we recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.

This constructor parses the given string as a fragment of XML. If the XML fragment is an element or attribute, you can bypass the root level rules for well-formed XML documents. This constructor can handle strings returned from ReadInnerXml.

The following table lists valid values for fragType and how the reader parses each of the different node types.

XmlNodeType

Fragment May Contain

Element

Any valid element content (for example, any combination of elements, comments, processing instructions, CDATA sections, text, and entity references).

An XML declaration can also be supplied. This allows you to specify the encoding for the XML fragment, rather than having to set it on the XmlParserContext object.

Attribute

The value of an attribute (the part inside the quotes).

Document

The contents of an entire XML document. This enforces document level rules.

The following example parses an XML fragment. It uses the XmlParserContext and its XmlNamespaceManager to handle namespace resolution.

Imports System
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main()

    'Create the XML fragment to be parsed.
    Dim xmlFrag as string ="<book> " & _
                           "<title>Pride And Prejudice</title>" & _
                           "<bk:genre>novel</bk:genre>" & _
                           "</book>" 

    'Create the XmlNamespaceManager.
    Dim nt as NameTable = new NameTable()
    Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(nt)
    nsmgr.AddNamespace("bk", "urn:sample")

    'Create the XmlParserContext.
    Dim context as XmlParserContext = new XmlParserContext(nothing, nsmgr, nothing, XmlSpace.None)

    'Create the reader. 
    Dim reader as XmlTextReader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context)

    'Parse the XML.  If they exist, display the prefix and  
    'namespace URI of each element.
    while (reader.Read())
      if (reader.IsStartElement())
        if (reader.Prefix=String.Empty)
           Console.WriteLine("<{0}>", reader.LocalName)
        else
            Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName)
            Console.WriteLine(" The namespace URI is " + reader.NamespaceURI)
        end if 
      end if
    end while

    'Close the reader.
    reader.Close()     

  end sub
end class

.NET Framework
Available since 1.1
Return to top
Show: