XmlDocument Constructor
Initializes a new instance of the XmlDocument class.
Assembly: System.Xml (in System.Xml.dll)
The following is an example of load-time validation. A document type definition (DTD) validating XmlReader 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 validating XmlReader to throw an exception and stop the load process when a validation error is found by not specifying the ValidationEventHandler. For more information about validating XML data with XmlReader, see Validating XML Data with XmlReader.
#using <System.Xml.dll> using namespace System; using namespace System::Xml; using namespace System::Xml::Schema; ref class XmlDocumentSample { private: static XmlReader^ reader; static String^ filename = "bookdtd.xml"; // Display the validation error. static void ValidationCallback(Object^ sender, ValidationEventArgs^ args) { Console::WriteLine("Validation error loading: {0}", filename); Console::WriteLine(args->Message); } public: static void Main() { ValidationEventHandler^ eventHandler = gcnew ValidationEventHandler(XmlDocumentSample::ValidationCallback); try { // Create the validating reader and specify DTD validation. XmlReaderSettings^ settings = gcnew XmlReaderSettings(); settings->DtdProcessing = DtdProcessing::Parse; settings->ValidationType = ValidationType::DTD; settings->ValidationEventHandler += eventHandler; reader = XmlReader::Create(filename, settings); // 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 = gcnew XmlDocument(); doc->Load(reader); Console::WriteLine(doc->OuterXml); } finally { if (reader != nullptr) reader->Close(); } } }; int main() { XmlDocumentSample::Main(); return 0; }
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>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.