XmlDocument 构造函数

定义

初始化 XmlDocument 类的新实例。

重载

XmlDocument()

初始化 XmlDocument 类的新实例。

XmlDocument(XmlImplementation)

使用指定的 XmlDocument 初始化 XmlImplementation 类的新实例。

XmlDocument(XmlNameTable)

使用指定的 XmlDocument 初始化 XmlNameTable 类的新实例。

XmlDocument()

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

初始化 XmlDocument 类的新实例。

public:
 XmlDocument();
public XmlDocument ();
Public Sub New ()

示例

下面是加载时验证的示例。 将 DTD) 验证 XmlReader (文档类型定义传递给 Load 方法,并提供 ValidationEventHandler 以通知用户任何验证错误。 在此示例中,发现验证错误,但仍加载文档。 或者,可以通过不指定 ValidationEventHandler来定义验证XmlReader以引发异常并停止加载进程。 有关验证 XML 数据的详细信息,请参阅 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;
}
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);
        }
    }
}
Option Explicit On
Option Strict On

Imports System.Xml
Imports System.Xml.Schema

Namespace Microsoft.Samples.Xml

    NotInheritable Class XmlDocumentSample

        Private Sub New()

        End Sub

        Shared reader As XmlReader
        Shared filename As String = "bookdtd.xml"

        Public Shared Sub Main()


            Dim eventHandler As New ValidationEventHandler(AddressOf XmlDocumentSample.ValidationCallback)

            Try

                ' Create the validating reader and specify DTD validation.
                Dim settings As New XmlReaderSettings()
                settings.DtdProcessing = DtdProcessing.Parse
                settings.ValidationType = ValidationType.DTD
                AddHandler 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.
                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.
        Private Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)
            Console.WriteLine("Validation error loading: {0}", filename)
            Console.WriteLine(args.Message)
        End Sub

    End Class
End Namespace

示例使用 bookDTD.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>

另请参阅

适用于

XmlDocument(XmlImplementation)

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

使用指定的 XmlDocument 初始化 XmlImplementation 类的新实例。

protected public:
 XmlDocument(System::Xml::XmlImplementation ^ imp);
protected internal XmlDocument (System.Xml.XmlImplementation imp);
new System.Xml.XmlDocument : System.Xml.XmlImplementation -> System.Xml.XmlDocument
Protected Friend Sub New (imp As XmlImplementation)

参数

imp
XmlImplementation

要使用的 XmlImplementation

适用于

XmlDocument(XmlNameTable)

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

使用指定的 XmlDocument 初始化 XmlNameTable 类的新实例。

public:
 XmlDocument(System::Xml::XmlNameTable ^ nt);
public XmlDocument (System.Xml.XmlNameTable nt);
new System.Xml.XmlDocument : System.Xml.XmlNameTable -> System.Xml.XmlDocument
Public Sub New (nt As XmlNameTable)

参数

nt
XmlNameTable

要使用的 XmlNameTable

适用于