共用方式為


使用 XML 命名空間

XmlSerializerNamespaces 類別的執行個體中包含的 XML 命名空間必須與名為<Namespaces in XML>的全球資訊網協會 (www.w3.org) 規格相符。

XML 命名空間會提供方法來限定 XML 文件中 XML 項目和 XML 屬性的名稱。限定名稱 (Qualified Name) 是由前置詞和本機名稱所組成,並以半形冒號 (;) 隔開。前置詞的功能只是替代符號 (Placeholder);它會對應到指定命名空間的 URI。通用管理的 URI 命名空間和本機名稱的組合會產生保證是通用唯一的名稱。

藉由建立 XmlSerializerNamespaces 的執行個體並將命名空間配對加入這個物件,您可指定用於 XML 文件中的前置詞。

若要建立 XML 文件中的限定名稱

  1. 建立 XmlSerializerNamespaces 類別的執行個體。

  2. 將所有前置詞和命名空間配對加入 XmlSerializerNamespaces 中。

  3. 將適合的 System.Xml.Serialization 屬性套用至 XmlSerializer 將序列化至 XML 文件的每個成員或類別。

    可用的屬性有:XmlAnyElementAttributeXmlArrayAttributeXmlArrayItemAttributeXmlAttributeAttributeXmlElementAttributeXmlRootAttributeXmlTypeAttribute

  4. 將每個屬性 (Attribute) 的 Namespace 屬性 (Property) 設定為 XmlSerializerNamespaces 的任一命名空間值。

  5. XmlSerializerNamespaces 傳遞至 XmlSerializerSerialize 方法。

下列範例會建立 XmlSerializerNamespaces 並將兩個前置詞和命名空間配對加入物件。程式碼會建立用來序列化 Books 類別執行個體的 XmlSerializer。程式碼同時會使用 XmlSerializerNamespaces 呼叫 Serialize 方法,允許 XML 包含前置的命名空間。

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
[C#]
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;
}

請參閱

XML 結構描述定義工具 | XML 序列化簡介 | XmlSerializer 類別 | 控制 XML 序列化的屬性 | XmlSerializerNamespaces 類別 | 覆寫 XML 序列化