XmlDocument.CreateDocumentType(String, String, String, String) Metoda

Definicja

Zwraca nowy XmlDocumentType obiekt.

public:
 virtual System::Xml::XmlDocumentType ^ CreateDocumentType(System::String ^ name, System::String ^ publicId, System::String ^ systemId, System::String ^ internalSubset);
public virtual System.Xml.XmlDocumentType CreateDocumentType (string name, string? publicId, string? systemId, string? internalSubset);
public virtual System.Xml.XmlDocumentType CreateDocumentType (string name, string publicId, string systemId, string internalSubset);
abstract member CreateDocumentType : string * string * string * string -> System.Xml.XmlDocumentType
override this.CreateDocumentType : string * string * string * string -> System.Xml.XmlDocumentType
Public Overridable Function CreateDocumentType (name As String, publicId As String, systemId As String, internalSubset As String) As XmlDocumentType

Parametry

name
String

Nazwa typu dokumentu.

publicId
String

Publiczny identyfikator typu dokumentu lub null. Można określić publiczny identyfikator URI, a także identyfikator systemu, aby zidentyfikować lokalizację zewnętrznego podzestawu DTD.

systemId
String

Identyfikator systemu typu dokumentu lub null. Określa adres URL lokalizacji pliku dla zewnętrznego podzestawu DTD.

internalSubset
String

Wewnętrzny podzbiór DTD typu dokumentu lub null.

Zwraca

Nowy XmlDocumentTypeelement .

Przykłady

Poniższy przykład tworzy węzeł DocumentType i dodaje go do dokumentu XML.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   
   //Create a document type node and  
   //add it to the document.
   XmlDocumentType^ doctype;
   doctype = doc->CreateDocumentType( "book", nullptr, nullptr, "<!ELEMENT book ANY>" );
   doc->AppendChild( doctype );
   
   //Create the root element and 
   //add it to the document.
   doc->AppendChild( doc->CreateElement( "book" ) );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();

    //Create a document type node and
    //add it to the document.
    XmlDocumentType doctype;
    doctype = doc.CreateDocumentType("book", null, null, "<!ELEMENT book ANY>");
    doc.AppendChild(doctype);

    //Create the root element and
    //add it to the document.
    doc.AppendChild(doc.CreateElement("book"));

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create the XmlDocument.
        Dim doc As New XmlDocument()
        
        'Create a document type node and  
        'add it to the document.
        Dim doctype As XmlDocumentType
        doctype = doc.CreateDocumentType("book", Nothing, Nothing, "<!ELEMENT book ANY>")
        doc.AppendChild(doctype)
        
        'Create the root element and 
        'add it to the document.
        doc.AppendChild(doc.CreateElement("book"))
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

Uwagi

Zwrócony węzeł będzie miał przeanalizowane Entities i Notations kolekcje.

Mimo że ta metoda tworzy nowy obiekt w kontekście dokumentu, nie dodaje automatycznie nowego obiektu do drzewa dokumentów. Aby dodać nowy obiekt, należy jawnie wywołać jedną z metod wstawiania węzła.

Zgodnie z zaleceniem W3C Extensible Markup Language (XML) 1.0 węzły DocumentType są dozwolone tylko w węzłach dokumentu. Każdy XmlDocument może mieć tylko jeden węzeł DocumentType. Węzeł DocumentType należy również wstawić przed elementem głównym elementu (jeśli dokument ma już element XmlDocument główny, nie można dodać węzła DocumentType). Jeśli przekazane parametry nie łączą się w celu utworzenia prawidłowego XmlDocumentTypeelementu , zgłaszany jest wyjątek.

Uwagi dotyczące dziedziczenia

Ta metoda ma zapotrzebowanie na dziedziczenie. Do zastąpienia CreateDocumentType metody jest wymagane pełne zaufanie.

Ta metoda jest rozszerzeniem firmy Microsoft do modelu obiektów dokumentów (DOM).

Dotyczy