HOW TO:限定 XML 項目和 XML 屬性名稱
Code Example
XmlSerializerNamespaces 類別執行個體所包含的 XML 命名空間,必須符合全球資訊網協會 (www.w3.org) 的<Namespaces in XML>規格。
XML 命名空間提供限定 XML 文件中 XML 項目和 XML 屬性名稱的方法。限定名稱 (Qualified Name) 是由前置詞和本機名稱所組成,並以半形冒號 (:) 隔開。前置詞的作用只是個替代符號 (Placeholder),它會對應到指定命名空間的 URI。通用管理的 URI 命名空間和本機名稱的組合會產生保證是通用唯一的名稱。
藉由建立 XmlSerializerNamespaces 執行個體以及將命名空間配對加入物件中,您可指定 XML 文件使用的前置詞。
若要在 XML 文件中建立限定名稱
建立 XmlSerializerNamespaces 類別的執行個體。
加入所有前置詞與命名空間配對至 XmlSerializerNamespaces。
套用適當的 System.Xml.Serialization 屬性至 XmlSerializer 將要序列化至 XML 文件的每個成員或類別。
可用的屬性為:XmlAnyElementAttribute、XmlArrayAttribute、XmlArrayItemAttribute、XmlAttributeAttribute、XmlElementAttribute、XmlRootAttribute 與 XmlTypeAttribute。
將每個屬性 (Attribute) 的 Namespace 屬性 (Property) 設定為 XmlSerializerNamespaces 的其中一個命名空間值。
傳遞 XmlSerializerNamespaces 至 XmlSerializer 的 Serialize 方法。
範例
下列範例建立 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
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;
}
請參閱
工作
HOW TO:指定 XML 資料流的替代項目名稱
HOW TO:序列化物件
HOW TO:還原序列化物件
參考
XmlSerializer Class
XmlSerializerNamespaces Class
概念
XML 結構描述定義工具和 XML 序列化
XML 序列化簡介
控制 XML 序列化的屬性
Copyright © 2007 by Microsoft Corporation. All rights reserved.