XmlSerializerNamespaces 类


包含 XmlSerializer 用于在 XML 文档实例中生成限定名的 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. 然后,该示例将 XmlSerializerNamespaces 传递到 Serialize 方法,该方法将 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.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.

XmlSerializerNamespaces 中包含的 XML 命名空间必须符合xml 中名为的 W3C 规范命名空间。XML 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. 限定名由前缀和本地名称组成,两者之间用冒号分隔。A qualified name consists of a prefix and a local name, separated by a colon. 前缀仅用作占位符;它将映射到用于指定命名空间的 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. XmlSerializerNamespaces 传递到 SerializeXmlSerializer 方法。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 类的新实例。Initializes a new instance of the XmlSerializerNamespaces class.


初始化 XmlSerializerNamespaces 类的新实例。Initializes a new instance of the XmlSerializerNamespaces class.


使用包含前缀和命名空间对集合的 XmlSerializerNamespaces 的指定实例,初始化 XmlSerializerNamespaces 类的新实例。Initializes a new instance of the XmlSerializerNamespaces class, using the specified instance of XmlSerializerNamespaces containing the collection of prefix and namespace pairs.



获取集合中前缀和命名空间对的数目。Gets the number of prefix and namespace pairs in the collection.


Add(String, String)

将前缀和命名空间对添加到 XmlSerializerNamespaces 对象。Adds a prefix and namespace pair to an XmlSerializerNamespaces object.


确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

获取 XmlSerializerNamespaces 对象中前缀和命名空间对的数组。Gets the array of prefix and namespace pairs in an XmlSerializerNamespaces object.


返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)