XmlSerializerNamespaces Constructor ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Initializes a new instance of the XmlSerializerNamespaces class.

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

public XmlSerializerNamespaces()

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)
      // 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);
   catch(Exception e)
      Console.WriteLine("Exception :" + e.Message + "Occured");

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top