XmlSerializerNamespaces Class

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

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

public ref class XmlSerializerNamespaces
public class XmlSerializerNamespaces
public class XmlSerializerNamespaces

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 www.w3.org specification named Namespaces in 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. 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 serializes into an XML document. The available attributes are:

  1. Set the Namespace property of each attribute to one of the namespace values from the XmlSerializerNamespaces object.

  2. 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:

No code example is currently available or this language may not be supported.

The following example creates an XmlSerializerNamespaces object, and adds two prefix and namespace 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.

#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()

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

public class Run
    public static void main(String[] args)
        Run test = new Run();
    } //main

    public void SerializeObject(String filename)
        XmlSerializer s = new XmlSerializer(Books.class.ToType());

        // 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 = Convert.ToDecimal(9.95);
        p.currency = "US Dollar";
        b.price = p;

        Books bks = new Books();
        bks.book = b;

        s.Serialize(t, bks, ns);
    } //SerializeObject
} //Run

public class Books
    /** @attribute XmlElement(Namespace = "http://www.cohowinery.com")
    public Book book;
} //Books

/** @attribute XmlType(Namespace = "http://www.cpandl.com")
public class Book
    /** @attribute XmlElement(Namespace = "http://www.cpandl.com")
    public String title;
    /** @attribute XmlElement(Namespace = "http://www.cohowinery.com")
    public Price price;
} //Book

public class Price
    /** @attribute XmlAttribute(Namespace = "http://www.cpandl.com")
    public String currency;
    /** @attribute XmlElement(Namespace = "http://www.cohowinery.com")
    public System.Decimal price;
} //Price


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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions