XmlDocument Constructors

Definition

Initializes a new instance of the XmlDocument class.

Overloads

XmlDocument()

Initializes a new instance of the XmlDocument class.

XmlDocument(XmlImplementation)

Initializes a new instance of the XmlDocument class with the specified XmlImplementation.

XmlDocument(XmlNameTable)

Initializes a new instance of the XmlDocument class with the specified XmlNameTable.

XmlDocument()

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Initializes a new instance of the XmlDocument class.

C#
public XmlDocument ();

Examples

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, see the Remarks section of the XmlReader reference page.

C#
using System;
using System.Xml;
using System.Xml.Schema;

namespace Microsoft.Samples.Xml
{
    sealed class XmlDocumentSample
    {
        private XmlDocumentSample() { }

        static XmlReader reader;
        static String filename = "bookdtd.xml";

        public static void Main()
        {

            ValidationEventHandler eventHandler = new ValidationEventHandler(XmlDocumentSample.ValidationCallback);

            try
            {
                // Create the validating reader and specify DTD validation.
                XmlReaderSettings settings = new 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 = new XmlDocument();
                doc.Load(reader);
                Console.WriteLine(doc.OuterXml);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }

        // Display the validation error.
        private static void ValidationCallback(object sender, ValidationEventArgs args)
        {
            Console.WriteLine("Validation error loading: {0}", filename);
            Console.WriteLine(args.Message);
        }
    }
}

The example uses the bookDTD.xml file as input.

XML
<!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>

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

XmlDocument(XmlImplementation)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Initializes a new instance of the XmlDocument class with the specified XmlImplementation.

C#
protected internal XmlDocument (System.Xml.XmlImplementation imp);

Parameters

imp
XmlImplementation

The XmlImplementation to use.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

XmlDocument(XmlNameTable)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Initializes a new instance of the XmlDocument class with the specified XmlNameTable.

C#
public XmlDocument (System.Xml.XmlNameTable nt);

Parameters

nt
XmlNameTable

The XmlNameTable to use.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0