Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
0 out of 2 rated this helpful - Rate this topic

XmlSerializerNamespaces Class

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

System.Object
  System.Xml.Serialization.XmlSerializerNamespaces

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)
public class XmlSerializerNamespaces

The XmlSerializerNamespaces type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsXmlSerializerNamespaces()Initializes a new instance of the XmlSerializerNamespaces class.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsXmlSerializerNamespaces(XmlQualifiedName[])Initializes a new instance of the XmlSerializerNamespaces class.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsXmlSerializerNamespaces(XmlSerializerNamespaces)Infrastructure. Initializes a new instance of the XmlSerializerNamespaces class, using the specified instance of XmlSerializerNamespaces containing the collection of prefix and namespace pairs.
Top
  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCountGets the number of prefix and namespace pairs in the collection.
Top
  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsAddAdds a prefix and namespace pair to an XmlSerializerNamespaces object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToArrayGets the array of prefix and namespace pairs in an XmlSerializerNamespaces object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

NoteNote

The creation of an empty namespace and prefix pair is not supported. That is, you cannot create a pair using the following code:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
ns.Add("", "");

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.