XmlSerializerNamespaces XmlSerializerNamespaces XmlSerializerNamespaces XmlSerializerNamespaces Class


將 XML 命名空間 (Namespace) 和 XmlSerializer 用來產生限定名稱的前置詞包含在 XML 文件執行個體中。Contains the XML namespaces and prefixes that the XmlSerializer uses to generate qualified names in an XML-document instance.

public ref class XmlSerializerNamespaces
public class XmlSerializerNamespaces
type XmlSerializerNamespaces = class
Public Class XmlSerializerNamespaces


下列範例會建立XmlSerializerNamespaces物件,並將兩個前置詞和命名空間配對加入其中。The following example creates an XmlSerializerNamespaces object, and adds two prefix and namespace pairs to it. 範例接著會傳遞XmlSerializerNamespacesSerialize方法,將序列化Books物件插入 XML 文件。The example then passes the XmlSerializerNamespaces to the Serialize method, which serializes a Books object into an XML document. 使用XmlSerializerNamespaces物件,Serialize方法限定每個 XML 項目和屬性的其中兩個命名空間。Using the XmlSerializerNamespaces object, the Serialize method qualifies each XML element and attribute with one of the two namespaces.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public ref class Price

   String^ currency;

   Decimal price;

public ref class Book

   String^ TITLE;

   Price^ PRICE;

public ref class Books

   Book^ Book;

public ref class Run
   static void main()
      Run^ test = gcnew Run;
      test->SerializeObject( "XmlNamespaces.xml" );

   void SerializeObject( String^ filename )
      XmlSerializer^ s = gcnew XmlSerializer( Books::typeid );

      // Writing a file requires a TextWriter.
      TextWriter^ t = gcnew StreamWriter( filename );

      /* Create an XmlSerializerNamespaces object and add two
            prefix-namespace pairs. */
      XmlSerializerNamespaces^ ns = gcnew XmlSerializerNamespaces;
      ns->Add( "books", "http://www.cpandl.com" );
      ns->Add( "money", "http://www.cohowinery.com" );

      // Create a Book instance.
      Book^ b = gcnew Book;
      b->TITLE = "A Book Title";
      Price^ p = gcnew Price;
      p->price = (Decimal)9.95;
      p->currency = "US Dollar";
      b->PRICE = p;
      Books^ bks = gcnew Books;
      bks->Book = b;
      s->Serialize( t, bks, ns );

int main()
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Run
   public static void Main()
      Run test = new Run();
   public void SerializeObject(string filename)
      XmlSerializer s = new XmlSerializer(typeof(Books));
      // Writing a file requires a TextWriter.
      TextWriter t = new StreamWriter(filename);

      /* Create an XmlSerializerNamespaces object and add two
      prefix-namespace pairs. */
      XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
      ns.Add("books", "http://www.cpandl.com");
      ns.Add("money", "http://www.cohowinery.com");

      // Create a Book instance.
      Book b = new Book();
      b.TITLE = "A Book Title";
      Price p = new Price();
      p.price = (decimal) 9.95;
      p.currency = "US Dollar";
      b.PRICE = p;
      Books bks = new Books();
      bks.Book = b;

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;

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

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

Public Class Run
    Public Shared Sub Main()
        Dim test As New Run()
    End Sub    
    Public Sub SerializeObject(ByVal filename As String)
        Dim s As New XmlSerializer(GetType(Books))
        ' Writing a file requires a TextWriter.
        Dim t As New StreamWriter(filename)
        ' Create an XmlSerializerNamespaces object and add two
        ' prefix-namespace pairs. 
        Dim ns As New XmlSerializerNamespaces()
        ns.Add("books", "http://www.cpandl.com")
        ns.Add("money", "http://www.cohowinery.com")
        ' Create a Book instance.
        Dim b As New Book()
        b.TITLE = "A Book Title"
        Dim p As New Price()
        p.price = CDec(9.95)
        p.currency = "US Dollar"
        b.PRICE = p
        Dim bks As New Books()
        bks.Book = b
        s.Serialize(t, bks, ns)
    End Sub
End Class

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

<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
    <XmlElement(Namespace := "http://www.cohowinery.com")> _
    Public price As Decimal
End Class


XmlSerializerNamespaces包含 XML 命名空間,每個都有相關聯的前置詞的集合。The XmlSerializerNamespaces contains a collection of XML namespaces, each with an associated prefix. XmlSerializer使用的執行個體XmlSerializerNamespaces類別來建立 XML 文件的限定的名稱。The XmlSerializer uses an instance of the XmlSerializerNamespaces class to create qualified names in an XML document.

所包含的 XML 命名空間XmlSerializerNamespaces必須符合名為 W3C 規格Namespaces in XMLXML namespaces contained by the XmlSerializerNamespaces must conform to the W3C specification named Namespaces in XML.

XML 命名空間提供限定的 XML 項目和 XML 文件中的屬性名稱的方式。XML namespaces provide a way to qualify the names of XML elements and 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.

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

  1. 建立XmlSerializerNamespaces執行個體。Create an XmlSerializerNamespaces instance.

  2. Add 每個您想要的執行個體的前置詞和命名空間配對。Add each prefix and namespace pair that you want to the instance.

  3. Appropriate.NET 屬性套用至每個屬性或類別XmlSerializer將序列化為 XML 文件。Apply the appropriate.NET attribute to each property or class that the XmlSerializer serializes into an XML document. 可用的屬性包括:The available attributes are:

  1. 設定Namespace屬性的每個屬性設定為其中一個命名空間值從XmlSerializerNamespaces物件。Set the Namespace property of each attribute to one of the namespace values from the XmlSerializerNamespaces object.

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


不支援空的命名空間和前置詞組的建立。The creation of an empty namespace and prefix pair is not supported. 也就是說,您無法建立一組,使用下列程式碼:That is, you cannot create a pair using the following code:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();   
ns.Add("", "");  
Dim ns As XmlSerializerNamespaces ns = New XmlSerializerNamespaces()  
ns.Add("", "")  


XmlSerializerNamespaces() XmlSerializerNamespaces() XmlSerializerNamespaces() XmlSerializerNamespaces()

初始化 XmlSerializerNamespaces 類別的新執行個體。Initializes a new instance of the XmlSerializerNamespaces class.

XmlSerializerNamespaces(XmlQualifiedName[]) XmlSerializerNamespaces(XmlQualifiedName[]) XmlSerializerNamespaces(XmlQualifiedName[]) XmlSerializerNamespaces(XmlQualifiedName[])

初始化 XmlSerializerNamespaces 類別的新執行個體。Initializes a new instance of the XmlSerializerNamespaces class.

XmlSerializerNamespaces(XmlSerializerNamespaces) XmlSerializerNamespaces(XmlSerializerNamespaces) XmlSerializerNamespaces(XmlSerializerNamespaces) XmlSerializerNamespaces(XmlSerializerNamespaces)

使用包含前置詞和命名空間配對集合之 XmlSerializerNamespaces 的指定執行個體,初始化 XmlSerializerNamespaces 類別的新執行個體。Initializes a new instance of the XmlSerializerNamespaces class, using the specified instance of XmlSerializerNamespaces containing the collection of prefix and namespace pairs.


Count Count Count Count

取得集合中前置詞和命名空間配對的數目。Gets the number of prefix and namespace pairs in the collection.


Add(String, String) Add(String, String) Add(String, String) Add(String, String)

將前置詞和命名空間配對加入 XmlSerializerNamespaces 物件。Adds a prefix and namespace pair to an XmlSerializerNamespaces object.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
ToArray() ToArray() ToArray() ToArray()

取得 XmlSerializerNamespaces 物件中前置詞和命名空間配對的陣列。Gets the array of prefix and namespace pairs in an XmlSerializerNamespaces object.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)