HOW TO:限定 XML 元素和 XML 屬性名稱How to: Qualify XML Element and XML Attribute Names

執行個體所包含的 XML 命名空間XmlSerializerNamespaces類別必須符合全球資訊網協會 (W3C) 規格Namespaces in XMLXML namespaces contained by instances of the XmlSerializerNamespaces class must conform to the World Wide Web Consortium (W3C) specification called Namespaces in XML.

XML 命名空間提供限定 XML 文件中 XML 項目和 XML 屬性名稱的方法。XML namespaces provide a method for qualifying the names of XML elements and XML attributes in XML documents. 限定名稱 (Qualified Name) 是由前置詞和本機名稱所組成,並以半形冒號 (:) 隔開。A qualified name consists of a prefix and a local name, separated by a colon. 前置詞的作用只是個替代符號 (Placeholder),它會對應到指定命名空間的 URI。The prefix functions only as a placeholder; it is mapped to a URI that specifies a namespace. 通用管理的 URI 命名空間和本機名稱的組合會產生保證是通用唯一的名稱。The combination of the universally managed URI namespace and the local name produces a name that is guaranteed to be universally unique.

藉由建立 XmlSerializerNamespaces 執行個體以及將命名空間配對加入物件中,您可指定 XML 文件使用的前置詞。By creating an instance of XmlSerializerNamespaces and adding the namespace pairs to the object, you can specify the prefixes used in an XML document.

若要在 XML 文件中建立限定名稱To create qualified names in an XML document

  1. 建立 XmlSerializerNamespaces 類別的執行個體。Create an instance of the XmlSerializerNamespaces class.

  2. 加入所有前置詞與命名空間配對至 XmlSerializerNamespacesAdd all prefixes and namespace pairs to the XmlSerializerNamespaces.

  3. 套用適當的 System.Xml.Serialization 屬性至 XmlSerializer 將要序列化至 XML 文件的每個成員或類別。Apply the appropriate System.Xml.Serialization attribute to each member or class that the XmlSerializer is to serialize into an XML document.

    可用的屬性為:XmlAnyElementAttributeXmlArrayAttributeXmlArrayItemAttributeXmlAttributeAttributeXmlElementAttributeXmlRootAttributeXmlTypeAttributeThe available attributes are: XmlAnyElementAttribute, XmlArrayAttribute, XmlArrayItemAttribute, XmlAttributeAttribute, XmlElementAttribute, XmlRootAttribute, and XmlTypeAttribute.

  4. 將每個屬性 (Attribute) 的 Namespace 屬性 (Property) 設定為 XmlSerializerNamespaces 的其中一個命名空間值。Set the Namespace property of each attribute to one of the namespace values from the XmlSerializerNamespaces.

  5. 傳遞 XmlSerializerNamespacesSerializeXmlSerializer 方法。Pass the XmlSerializerNamespaces to the Serialize method of the XmlSerializer.

範例Example

下列範例建立 XmlSerializerNamespaces,並在物件加入兩個前置詞和命名空間配對。The following example creates an XmlSerializerNamespaces, and adds two prefix and namespace pairs to the object. 程式碼建立用來系列化 XmlSerializer 類別執行個體的 BooksThe code creates an XmlSerializer that is used to serialize an instance of the Books class. 程式碼以 Serialize``XmlSerializerNamespaces呼叫 方法,讓 XML 能包含有前置詞的命名空間。The code calls the Serialize method with the XmlSerializerNamespaces, allowing the XML to contain prefixed namespaces.

Option Explicit
public class Price
{
    [XmlAttribute(Namespace = "http://www.cpandl.com")]
    public string currency;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal price;
}

Option Strict

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

Public Class Run

    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("XmlNamespaces.xml")
    End Sub 'Main

    Public Sub SerializeObject(filename As String)
        Dim mySerializer As New XmlSerializer(GetType(Books))
        ' Writing a file requires a TextWriter.
        Dim myWriter As New StreamWriter(filename)

        ' Creates an XmlSerializerNamespaces and adds two
        ' prefix-namespace pairs.
        Dim myNamespaces As New XmlSerializerNamespaces()
        myNamespaces.Add("books", "http://www.cpandl.com")
        myNamespaces.Add("money", "http://www.cohowinery.com")

        ' Creates a Book.
        Dim myBook As New Book()
        myBook.TITLE = "A Book Title"
        Dim myPrice As New Price()
        myPrice.price = CDec(9.95)
        myPrice.currency = "US Dollar"
        myBook.PRICE = myPrice
        Dim myBooks As New Books()
        myBooks.Book = myBook
        mySerializer.Serialize(myWriter, myBooks, myNamespaces)
        myWriter.Close()
    End Sub
End Class

Public Class Books
    <XmlElement([Namespace] := "http://www.cohowinery.com")> _
    Public Book As Book
End Class 'Books

<XmlType([Namespace] := "http://www.cpandl.com")> _
Public Class Book

    <XmlElement([Namespace] := "http://www.cpandl.com")> _
    Public TITLE As String
    <XmlElement([Namespace] := "http://www.cohowinery.com")> _
    Public PRICE As Price
End Class

Public Class Price
    <XmlAttribute([Namespace] := "http://www.cpandl.com")> _
    Public currency As String
    Public <XmlElement([Namespace] := "http://www.cohowinery.com")> _
        price As Decimal
End Class
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Run
{
    public static void Main()
    {
        Run test = new Run();
        test.SerializeObject("XmlNamespaces.xml");
    }
    public void SerializeObject(string filename)
    {
        XmlSerializer mySerializer = new XmlSerializer(typeof(Books));
        // Writing a file requires a TextWriter.
        TextWriter myWriter = new StreamWriter(filename);

        // Creates an XmlSerializerNamespaces and adds two
        // prefix-namespace pairs.
        XmlSerializerNamespaces myNamespaces =
        new XmlSerializerNamespaces();
        myNamespaces.Add("books", "http://www.cpandl.com");
        myNamespaces.Add("money", "http://www.cohowinery.com");

        // Creates a Book.
        Book myBook = new Book();
        myBook.TITLE = "A Book Title";
        Price myPrice = new Price();
        myPrice.price = (decimal) 9.95;
        myPrice.currency = "US Dollar";
        myBook.PRICE = myPrice;
        Books myBooks = new Books();
        myBooks.Book = myBook;
        mySerializer.Serialize(myWriter,myBooks,myNamespaces);
        myWriter.Close();
    }
}

public class Books
{
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public Book Book;
}

[XmlType(Namespace ="http://www.cpandl.com")]
public class Book
{
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string TITLE;
    [XmlElement(Namespace ="http://www.cohowinery.com")]
    public Price PRICE;
}

另請參閱See also