XmlDocument.CreateXmlDeclaration(String, String, String) Metódus

Definíció

Létrehoz egy csomópontot XmlDeclaration a megadott értékekkel.

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

Paraméterek

version
String

A verziónak "1.0"-nak kell lennie.

encoding
String

A kódolási attribútum értéke. Ez az a kódolás, amelyet a fájlba vagy streambe való mentéskor XmlDocument használnak, ezért az osztály által Encoding támogatott sztringre kell állítani, ellenkező esetben Save(String) sikertelen. Ha ez vagy null String.Empty, a Save metódus nem ír kódolási attribútumot az XML-deklarációra, ezért a rendszer az alapértelmezett UTF-8 kódolást használja.

Megjegyzés: Ha a XmlDocument rendszer egy vagy egy TextWriterfájlba XmlTextWriter menti a kódolási értéket, a rendszer elveti ezt a kódolási értéket. Ehelyett a kódolást TextWriter használja a rendszer XmlTextWriter . Ez biztosítja, hogy a kiírt XML a megfelelő kódolással visszaolvasható legyen.

standalone
String

Az értéknek "igen" vagy "nem" értékűnek kell lennie. Ha ez vagy null String.Empty, a Save metódus nem ír önálló attribútumot az XML-deklarációra.

Válaszok

Az új XmlDeclaration csomópont.

Kivételek

Az értékek a version fent megadottaktól eltérőek.standalone

Példák

Az alábbi példa létrehoz egy XML-deklarációt, és hozzáadja a dokumentumhoz.

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

Megjegyzések

Az attribútumok speciális tulajdonságokként jelennek meg a XmlDeclaration csomóponton, és nem csomópontokként XmlAttribute .

Bár ez a metódus a dokumentum kontextusában hozza létre az új objektumot, nem adja hozzá automatikusan az új objektumot a dokumentumfához. Az új objektum hozzáadásához explicit módon meg kell hívnia a csomópont beszúrási módszereinek egyikét.

A W3C bővíthető korrektúranyelv (XML) 1.0-s javaslata szerint a XmlDeclaration csomópontnak kell lennie a dokumentum első csomópontjának.

Ez a metódus a Dokumentumobjektum-modell (DOM) Microsoft bővítménye.

A következőre érvényes:

Lásd még