System.Xml.Serialization.XmlSerializerNamespaces Class

Assembly: System.Xml.dll
Namespace: System.Xml.Serialization
Summary
Contains the XML namespaces and prefixes that the XmlSerializer uses to generate qualified names in an XML-document instance.
C# Syntax:
public class XmlSerializerNamespaces
Remarks
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 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. XmlSerializerNamespaces.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 XmlSerializer.Serialize method of the XmlSerializer.
Example
The following example creates an XmlSerializerNamespaces object, and adds two prefix-namespaces pairs to it. The example then passes the XmlSerializerNamespaces to the XmlSerializer.Serialize method, which serializes a Books object into an XML document. Using the XmlSerializerNamespaces object, the XmlSerializer.Serialize method qualifies each XML element and attribute with one of the two namespaces.
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
 
public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("XmlNamespaces.xml");
   }
 
   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;
      s.Serialize(t,bks,ns);
      t.Close();
   }
}

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


    
See also:
System.Xml.Serialization Namespace

System.Xml.Serialization.XmlSerializerNamespaces Member List:

Public Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the XmlSerializerNamespaces class.
ctor #2 Overloaded:
.ctor(XmlQualifiedName[] namespaces)

Initializes a new instance of the XmlSerializerNamespaces class.
ctor #3 Overloaded:
.ctor(XmlSerializerNamespaces namespaces)

Initializes a new instance of the XmlSerializerNamespaces class, using the specified array of XmlQualifiedName objects to create a collection of prefix-namespace pairs.
Public Properties
Count Read-only

Gets the number of prefix-namespace pairs in the collection.
Public Methods
Add Adds a prefix-namespace pair to an XmlSerializerNamespaces object.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToArray Gets the array of prefix-namespace pairs in an XmlSerializerNamespaces object.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.Serialization.XmlSerializerNamespaces Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the XmlSerializerNamespaces class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public XmlSerializerNamespaces();
Example
The following example creates an instance of the XmlSerializerNamespaces class, and adds a prefix and namespace pair to the object.
   private void CreateBook(string filename)
   {
      try
      {
         // Create instance of XmlSerializerNamespaces and add the namespaces.
         XmlSerializerNamespaces myNameSpaces = new XmlSerializerNamespaces();
         myNameSpaces.Add("BookName", "http://www.cpandl.com");
      
         // Create instance of XmlSerializer and specify the type of object
         // to be serialized.
         XmlSerializer mySerializerObject = 
            new XmlSerializer(typeof(MyBook));

         TextWriter myWriter = new StreamWriter(filename);
         // Create object to be serialized.
         MyBook myXMLBook = new MyBook();
      
         myXMLBook.Author = "XMLAuthor";
         myXMLBook.BookName = "DIG THE XML";
         myXMLBook.Description = "This is a XML Book";

         MyPriceClass myBookPrice = new MyPriceClass();
         myBookPrice.Price = (decimal) 45.89;
         myBookPrice.Units = "$";
         myXMLBook.BookPrice = myBookPrice;

         // Serialize the object.
         mySerializerObject.Serialize(myWriter, myXMLBook,myNameSpaces);
         myWriter.Close();
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception :" + e.Message + "Occured");
      }
   }

    

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the XmlSerializerNamespaces class.
C# Syntax:
public XmlSerializerNamespaces(
   XmlQualifiedName[] namespaces
);
Parameters:

namespaces

An array of XmlQualifiedName objects.

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the XmlSerializerNamespaces class, using the specified array of XmlQualifiedName objects to create a collection of prefix-namespace pairs.
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public XmlSerializerNamespaces(XmlSerializerNamespaces(
   XmlSerializerNamespaces namespaces
);
Parameters:

namespaces

An array of XmlQualifiedName objects.

Example
The following example creates two XmlQualifiedName objects, and creates a new XmlSerializerNamespaces instance from them.
private XmlSerializerNamespaces CreateFromQNames()
{
   XmlQualifiedName q1 = 
   new XmlQualifiedName("money", "http://www.cohowinery.com");
        
   XmlQualifiedName q2 = 
   new XmlQualifiedName("books", "http://www.cpandl.com");
        
   XmlQualifiedName[] names = {q1, q2};

   return new XmlSerializerNamespaces(names);
}


    

Return to top


Property: Count (read-only)
Summary
Gets the number of prefix-namespace pairs in the collection.
C# Syntax:
public int Count {get;}

Return to top


Method: Add(
   string prefix,
   string ns
)
Summary
Adds a prefix-namespace pair to an XmlSerializerNamespaces object.
C# Syntax:
public void Add(
   string prefix,
   string ns
);
Parameters:

prefix

The prefix associated with an XML namespace.

ns

An XML namespace.

Remarks
If you want the XmlSerializer to qualify the element and attribute names in an XML document, you must XmlSerializerNamespaces.Add the prefix-namespace pairs to an XmlSerializerNamespaces object.

Any namespaces that you add must conform to the www.w3.org specification Namespaces in XML.

Example
The following example creates an XmlSerializerNamespaces object, and adds three prefix-namespace pairs to it by calling the XmlSerializerNamespaces.Add method.
private XmlSerializerNamespaces AddNamespaces()
{
   XmlSerializerNamespaces xmlNamespaces = 
   new XmlSerializerNamespaces();

   // Add three prefix-namespace pairs.
   xmlNamespaces.Add("money", "http://www.cpandl.com");
   xmlNamespaces.Add("books", "http://www.cohowinery.com");
   xmlNamespaces.Add("software", "http://www.microsoft.com");

   return xmlNamespaces;
}


    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlSerializerNamespaces();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToArray()
Summary
Gets the array of prefix-namespace pairs in an XmlSerializerNamespaces object.
C# Syntax:
public XmlQualifiedName[] ToArray();
Return Value:
An array of XmlQualifiedName objects that are used as qualified names in an XML document.
Example
The following example prints each XmlQualifiedName.Name and XmlQualifiedName.Namespace in an XmlSerializerNamespaces object.
private void PrintNamespacePairs(XmlSerializerNamespaces namespaces)
{
   XmlQualifiedName[] qualifiedNames = namespaces.ToArray();
   for(int i = 0; i < qualifiedNames.Length; i++)
   {
      Console.WriteLine
      (
      qualifiedNames[i].Name + "\t" +
      qualifiedNames[i].Namespace
      );
   }
}


    

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.