XmlDocument Constructor
Initializes a new instance of the XmlDocument class.
Overload List
Initializes a new instance of the XmlDocument class.
Supported by the .NET Compact Framework.
[Visual Basic] Public Sub New()
[C#] public XmlDocument();
[C++] public: XmlDocument();
[JScript] public function XmlDocument();
Initializes a new instance of the XmlDocument class with the specified XmlImplementation.
[Visual Basic] Protected Friend Sub New(XmlImplementation)
[C#] protected internal XmlDocument(XmlImplementation);
[C++] protected public: XmlDocument(XmlImplementation*);
[JScript] protected internal function XmlDocument(XmlImplementation);
Initializes a new instance of the XmlDocument class with the specified XmlNameTable.
Supported by the .NET Compact Framework.
[Visual Basic] Public Sub New(XmlNameTable)
[C#] public XmlDocument(XmlNameTable);
[C++] public: XmlDocument(XmlNameTable*);
[JScript] public function XmlDocument(XmlNameTable);
Example
[Visual Basic, C#, C++] The following is an example of load-time validation. An XmlValidatingReader is passed to the Load method and a ValidationEventHandler is provided to notify users of any validation errors. In this example a validation error is found, but the document is still loaded. Alternatively, you can define a ValidationEventHandler to throw an exception and stop the load process when a validation error is found.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of the XmlDocument constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] Option Explicit Option Strict Imports System Imports System.IO Imports System.Xml Imports System.Xml.Schema Public Class Sample Private Shared reader As XmlValidatingReader = Nothing Private Shared txtReader As XmlTextReader = Nothing Private Shared filename As String = "bookDTD.xml" Public Shared Sub Main() Try ' Create the validating reader and specify DTD validation. txtReader = New XmlTextReader(filename) reader = New XmlValidatingReader(txtReader) reader.ValidationType = ValidationType.DTD ' Set a handler to handle validation errors. AddHandler reader.ValidationEventHandler, AddressOf ValidationCallBack ' Pass the validating reader to the XML document. ' Validation fails due to an undefined attribute, but the ' data is still loaded into the document. Dim doc As New XmlDocument() doc.Load(reader) Console.WriteLine(doc.OuterXml) Finally If Not (reader Is Nothing) Then reader.Close() End If End Try End Sub ' Display the validation error. Shared Sub ValidationCallback(sender As Object, args As ValidationEventArgs) Console.WriteLine("Validation error loading: {0}", filename) Console.WriteLine(args.Message) End Sub End Class [C#] using System; using System.IO; using System.Xml; using System.Xml.Schema; public class Sample { static XmlValidatingReader reader = null; static XmlTextReader txtReader = null; static String filename = "bookDTD.xml"; public static void Main() { ValidationEventHandler eventHandler = new ValidationEventHandler(Sample.ValidationCallback); try { // Create the validating reader and specify DTD validation. txtReader = new XmlTextReader(filename); reader = new XmlValidatingReader(txtReader); reader.ValidationType = ValidationType.DTD; // Set a handler to handle validation errors. reader.ValidationEventHandler += eventHandler; // Pass the validating reader to the XML document. // Validation fails due to an undefined attribute, but the // data is still loaded into the document. XmlDocument doc = new XmlDocument(); doc.Load(reader); Console.WriteLine(doc.OuterXml); } finally { if ( reader != null ) reader.Close(); } } // Display the validation error. static void ValidationCallback(object sender, ValidationEventArgs args ) { Console.WriteLine("Validation error loading: {0}", filename); Console.WriteLine(args.Message); } } [C++] #using <mscorlib.dll> #using <System.dll> #using <System.Xml.dll> using namespace System; using namespace System::IO; using namespace System::Xml; using namespace System::Xml::Schema; public __gc class Sample { public: static String* filename = S"bookDTD.xml"; // Display the validation error. static void ValidationCallback(Object* /*sender*/, ValidationEventArgs* args ) { Console::WriteLine("Validation error loading: {0}", filename); Console::WriteLine(args->Message); } }; int main() { ValidationEventHandler* eventHandler = new ValidationEventHandler(0, Sample::ValidationCallback); XmlTextReader* txtReader; XmlValidatingReader* reader; try { // Create the validating reader and specify DTD validation. txtReader = new XmlTextReader( Sample::filename ); reader = new XmlValidatingReader( txtReader); reader->ValidationType = ValidationType::DTD; // Set a handler to handle validation errors. reader->ValidationEventHandler += eventHandler; // Pass the validating reader to the XML document. // Validation fails due to an undefined attribute, but the // data is still loaded into the document. XmlDocument* doc = new XmlDocument(); doc->Load( reader ); Console::WriteLine( doc->OuterXml ); } __finally { if ( reader != 0 ) reader->Close(); } }
The example uses the bookDTD.xml file as input.
<!DOCTYPE bookstore [
<!ELEMENT bookstore (book)*>
<!ELEMENT book (title,author,price)>
<!ATTLIST book genre CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>]>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
See Also
XmlDocument Class | XmlDocument Members | System.Xml Namespace