XmlDocument.CreateXmlDeclaration(String, String, String) Metoda

Definicja

XmlDeclaration Tworzy węzeł z określonymi wartościami.

public:
 virtual System::Xml::XmlDeclaration ^ CreateXmlDeclaration(System::String ^ version, System::String ^ encoding, System::String ^ standalone);
public virtual System.Xml.XmlDeclaration CreateXmlDeclaration (string version, string encoding, string standalone);
public virtual System.Xml.XmlDeclaration CreateXmlDeclaration (string version, string? encoding, string? standalone);
abstract member CreateXmlDeclaration : string * string * string -> System.Xml.XmlDeclaration
override this.CreateXmlDeclaration : string * string * string -> System.Xml.XmlDeclaration
Public Overridable Function CreateXmlDeclaration (version As String, encoding As String, standalone As String) As XmlDeclaration

Parametry

version
String

Wersja musi mieć wartość "1.0".

encoding
String

Wartość atrybutu kodowania. Jest to kodowanie, które jest używane podczas zapisywania XmlDocument pliku lub strumienia, dlatego musi być ustawione na ciąg obsługiwany przez Encoding klasę, w przeciwnym razie Save(String) kończy się niepowodzeniem. Jeśli jest null to lub String.Empty, Save metoda nie zapisuje atrybutu kodowania w deklaracji XML i dlatego jest używane domyślne kodowanie UTF-8.

Uwaga: jeśli element XmlDocument jest zapisywany w TextWriter obiekcie lub XmlTextWriter, ta wartość kodowania zostanie odrzucona. Zamiast tego używane jest kodowanie TextWriter obiektu lub XmlTextWriter . Dzięki temu można odczytać zapisany kod XML przy użyciu poprawnego kodowania.

standalone
String

Wartość musi mieć wartość "yes" lub "no". Jeśli jest null to lub String.Empty, Save metoda nie zapisuje atrybutu autonomicznego w deklaracji XML.

Zwraca

Nowy XmlDeclaration węzeł.

Wyjątki

Wartości version lub standalone są czymś innym niż określone powyżej.

Przykłady

Poniższy przykład tworzy deklarację XML i dodaje ją do dokumentu.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>" );
   
   //Create an XML declaration. 
   XmlDeclaration^ xmldecl;
   xmldecl = doc->CreateXmlDeclaration( "1.0", nullptr, nullptr );
   
   //Add the new node to the document.
   XmlElement^ root = doc->DocumentElement;
   doc->InsertBefore( xmldecl, root );
   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()
  {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create an XML declaration.
    XmlDeclaration xmldecl;
    xmldecl = doc.CreateXmlDeclaration("1.0",null,null);

    //Add the new node to the document.
    XmlElement root = doc.DocumentElement;
    doc.InsertBefore(xmldecl, root);

    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()
        Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _
                    "</book>")
        
        'Create an XML declaration. 
        Dim xmldecl As XmlDeclaration
        xmldecl = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
        
        'Add the new node to the document.
        Dim root As XmlElement = doc.DocumentElement
        doc.InsertBefore(xmldecl, root)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

Uwagi

Atrybuty są widoczne jako właściwości specjalne w węźle XmlDeclaration , a nie jako XmlAttribute węzły.

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 XmlDeclaration W3C Extensible Markup Language (XML) 1.0 węzeł musi być pierwszym węzłem w dokumencie.

Ta metoda jest rozszerzeniem Microsoft do modelu DOM (Document Object Model).

Dotyczy

Zobacz też