This documentation is archived and is not being maintained.

How to: Qualify XML Element and XML Attribute Names

Code Example

XML namespaces contained by instances of the XmlSerializerNamespaces class must conform to the World Wide Web Consortium ( specification called "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.

By creating an instance of XmlSerializerNamespaces and adding the namespace pairs to the object, you can specify the prefixes used in an XML document.

To create qualified names in an XML document

  1. Create an instance of the XmlSerializerNamespaces class.

  2. Add all prefixes and namespace pairs to the XmlSerializerNamespaces.

  3. Apply the appropriate System.Xml.Serialization attribute to each member or class that the XmlSerializer is to serialize into an XML document.

    The available attributes are: XmlAnyElementAttribute, XmlArrayAttribute, XmlArrayItemAttribute, XmlAttributeAttribute, XmlElementAttribute, XmlRootAttribute, and XmlTypeAttribute.

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

  5. Pass the XmlSerializerNamespaces to the Serialize method of the XmlSerializer.


The following example creates an XmlSerializerNamespaces, and adds two prefix and namespace pairs to the object. The code creates an XmlSerializer that is used to serialize an instance of the Books class. The code calls the Serialize method with the XmlSerializerNamespaces, allowing the XML to contain prefixed namespaces.

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 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", "");
        myNamespaces.Add("money", "");

        // 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;

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

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

See Also