XmlSerializerNamespaces.XmlSerializerNamespaces() Constructor

Initializes a new instance of the XmlSerializerNamespaces class.

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

public XmlSerializerNamespaces ()
public XmlSerializerNamespaces ()
public function XmlSerializerNamespaces ()
Not applicable.

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

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(
            MyBook.class.ToType());

        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 = System.Convert.ToDecimal(45.89);
        mybookPrice.units = "$";
        myXMLBook.bookPrice = mybookPrice;
        // Serialize the object.
        mySerializerObject.Serialize(myWriter, myXMLBook, myNameSpaces);
        myWriter.Close();
    }
    catch (System.Exception e) {
        Console.WriteLine("Exception :" + e.get_Message() + "Occured");
    }
} //CreateBook

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show: