The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XmlSerializerNamespaces Class

Contains the XML namespaces and prefixes that the XmlSerializer uses to generate qualified names in an XML-document instance.

For a list of all members of this type, see XmlSerializerNamespaces Members.


[Visual Basic]
Public Class XmlSerializerNamespaces
public class XmlSerializerNamespaces
public __gc class XmlSerializerNamespaces
public class XmlSerializerNamespaces

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


The XmlSerializerNamespaces contains a collection of XML namespaces, each with an associated prefix. The XmlSerializer uses an instance of the XmlSerializerNamespaces class to create qualified names in an XML document.

XML namespaces contained by the XmlSerializerNamespaces must conform to the specification named Namespaces in XML.

XML namespaces provide a method for qualifying the names of XML elements and XML attributes in XML documents. A qualified name consists of a prefix and a local name, separated by a colon. The prefix functions only as a placeholder; it is mapped to a URI that specifies a namespace. The combination of the universally-managed URI namespace and the local name produces a name that is guaranteed to be universally unique.

To create qualified names in an XML document:

  1. Create an XmlSerializerNamespaces instance.
  2. Add each prefix and namespace pair that you want to the instance.
  3. Apply the appropriate .NET attribute to each property or class that the XmlSerializer will serialize into an XML document. The available attributes are: XmlArrayAttribute, XmlArrayItemAttribute, XmlAttributeAttribute, XmlElementAttribute, XmlRootAttribute, XmlTypeAttribute.
  4. Set the Namespace property of each attribute to one of the namespace values from the XmlSerializerNamespaces object.
  5. Pass the XmlSerializerNamespaces to the Serialize method of the XmlSerializer.


[Visual Basic, C#, C++] The following example creates an XmlSerializerNamespaces object, and adds two prefix-namespaces pairs to it. The example then passes the XmlSerializerNamespaces to the Serialize method, which serializes a Books object into an XML document. Using the XmlSerializerNamespaces object, the Serialize method qualifies each XML element and attribute with one of the two namespaces.

[Visual Basic] 
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", "")
        ns.Add("money", "")
        ' 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 := "")> _
    Public Book As Book
End Class

<XmlType(Namespace := "")> _
Public Class Book
    <XmlElement(Namespace := "")> _
    Public TITLE As String

    <XmlElement(Namespace := "")> _
    Public PRICE As Price
End Class

Public Class Price
    <XmlAttribute(Namespace := "")> _
    Public currency As String
    <XmlElement(Namespace := "")> _
    Public 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();
   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", "");
      ns.Add("money", "");

      // 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 = "")]
   public Book Book;

[XmlType(Namespace ="")]
public class Book
   [XmlElement(Namespace = "")]
   public string TITLE;
   [XmlElement(Namespace ="")]
   public Price PRICE;

public class Price
   [XmlAttribute(Namespace = "")]
   public string currency;
   [XmlElement(Namespace = "")]
   public decimal price;

#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public __gc class Price
   [XmlAttributeAttribute(Namespace = S"")]
   String* currency;
   [XmlElement(Namespace = S"")]
   Decimal price;
[XmlType(Namespace =S"")]
public __gc class Book
   [XmlElement(Namespace = S"")]
   String* TITLE;
   [XmlElement(Namespace =S"")]
   Price* PRICE;

public __gc class Books
   [XmlElement(Namespace = S"")]
   Book* Book;

public __gc class Run
   static void main()
      Run* test = new Run();
   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(S"books", S"");
      ns->Add(S"money", S"");

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

int main()

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Xml.Serialization

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Xml (in System.Xml.dll)

See Also

XmlSerializerNamespaces Members | System.Xml.Serialization Namespace

© 2014 Microsoft