XmlDocument 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 XmlDocument 類別的新執行個體。
多載
XmlDocument() |
初始化 XmlDocument 類別的新執行個體。 |
XmlDocument(XmlImplementation) |
使用指定的 |
XmlDocument(XmlNameTable) |
使用指定的 |
XmlDocument()
初始化 XmlDocument 類別的新執行個體。
public:
XmlDocument();
public XmlDocument ();
Public Sub New ()
範例
以下是載入時間驗證的範例。 檔案類型定義 (DTD) 驗證 XmlReader 傳遞至 Load 方法,並提供 來 ValidationEventHandler 通知使用者任何驗證錯誤。 在此範例中,發現驗證錯誤,但檔仍會載入。 或者,您可以定義驗證 XmlReader 以擲回例外狀況,並在找不到驗證錯誤時停止載入進程,方法是未指定 ValidationEventHandler 。 如需驗證 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)
使用指定的 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)
參數
要使用的 XmlImplementation
。
適用於
XmlDocument(XmlNameTable)
使用指定的 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
。