Export (0) Print
Expand All

XmlValidatingReader.XmlValidatingReader(String, XmlNodeType, XmlParserContext) Constructor

Initializes a new instance of the XmlValidatingReader class with the specified values.

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

public XmlValidatingReader (
	string xmlFragment,
	XmlNodeType fragType,
	XmlParserContext context
)
public XmlValidatingReader (
	String xmlFragment, 
	XmlNodeType fragType, 
	XmlParserContext context
)
public function XmlValidatingReader (
	xmlFragment : String, 
	fragType : XmlNodeType, 
	context : XmlParserContext
)
Not applicable.

Parameters

xmlFragment

The string containing the XML fragment to parse.

fragType

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

context

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

Exception typeCondition

XmlException

fragType is not one of the node types listed in the table below.

NoteNote:

The XmlValidatingReader class is obsolete in Microsoft .NET Framework version 2.0. You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see Validating XML Data with XmlReader.

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, text, and entity references).

Attribute

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

Document

The contents of an entire XML document; this enforces document level rules.

If this reader will be validating by using document type definition (DTD) (that is, ValidationType is set to ValidationType.DTD or ValidationType.Auto), the XmlParserContext specified in the constructor must supply all the necessary DocumentType information.

NoteNote:

It is not possible to validate a fragment by using DTD. By definition DTD requires an entire document to be loaded for validation.

If this reader will be validating by using XML-Data Reduced (XDR) or XML Schema definition language (XSD) schemas, use the Schemas property to specify the XmlSchemaCollection that contains the schemas (the XmlParserContext does not need to supply the DocumentType information).

The following example reads an XML fragment. It uses an XmlParserContext and its XmlNamespaceManager to handle namespace matching.

using System;
using System.IO;
using System.Xml;

public class Sample
{
    public static void Main()
    {
        XmlTextReader reader = null;

        try
        {
            //Create the XML fragment to be parsed.
            string xmlFrag = "<book> " +
                            "<title>Pride And Prejudice</title>" +
                            "<bk:genre>novel</bk:genre>" +
                            "</book>";

            //Create the XmlNamespaceManager that is used to
            //look up namespace information.
            NameTable nt = new NameTable();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
            nsmgr.AddNamespace("bk", "urn:sample");

            //Create the XmlParserContext.
            XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

            //Implement the reader. 
            reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

            //Parse the XML fragment.  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);
                    }
                }
            }


        }

        finally
        {
            if (reader != null)
                reader.Close();
        }
    }
} // End class

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft