XmlDocument.CreateXmlDeclaration(String, String, String) 方法

定義

建立具有指定值的 XmlDeclaration 節點。

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

參數

version
String

版本必須是 "1.0"。

encoding
String

編碼屬性的值。 這是在您將 XmlDocument 儲存至檔案或資料流時使用的編碼方式,因此,必須設定為 Encoding 類別支援的字串,否則 Save(String) 會失敗。 如果這是 null 或 String.Empty,Save 方法不會在 XML 宣告上寫入編碼屬性,因此會使用預設編碼方式 UTF-8。

注意:如果 XmlDocument 儲存至 TextWriterXmlTextWriter,則會捨棄這個編碼值。 改用 TextWriterXmlTextWriter 的編碼方式。 這可以確保寫出的 XML 可以使用正碼的編碼方式讀回。

standalone
String

值必須為「是」或「否」。 如果這是 null 或 String.Empty,Save 方法不會在 XML 宣告上寫入獨立屬性。

傳回

新的 XmlDeclaration 節點。

例外狀況

versionstandalone 的值非上述指定的值。

範例

下列範例會建立 XML 宣告,並將其新增至檔。

#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

備註

屬性會公開為節點上的特殊屬性 XmlDeclaration ,而不是做為 XmlAttribute 節點。

雖然這個方法會在檔的內容中建立新的 物件,但不會自動將新物件加入檔樹狀結構。 若要新增物件,您必須明確呼叫其中一個節點插入方法。

根據 W3C Extensible Markup Language (XML) 1.0 建議XmlDeclaration 節點必須是檔中的第一個節點。

這個方法是檔物件模型 (DOM) Microsoft延伸模組。

適用於

另請參閱