This documentation is archived and is not being maintained.

XmlNamespaceDeclarationsAttribute Class

Specifies that the target property, parameter, return value or class member contains prefixes associated with namespaces that are used within an XML document.

For a list of all members of this type, see XmlNamespaceDeclarationsAttribute Members.


[Visual Basic]
<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field _
   Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue)>
Public Class XmlNamespaceDeclarationsAttribute
   Inherits Attribute
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field
   | AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class XmlNamespaceDeclarationsAttribute : Attribute
[AttributeUsage(AttributeTargets::Property |
   AttributeTargets::Field | AttributeTargets::Parameter |
public __gc class XmlNamespaceDeclarationsAttribute : public
   AttributeUsage(AttributeTargets.Property | AttributeTargets.Field |
   AttributeTargets.Parameter | AttributeTargets.ReturnValue)
class XmlNamespaceDeclarationsAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


The XmlNamespaceDeclarationsAttribute attribute can only be applied once in a class to a field or property that returns an XmlSerializerNamespaces object.

The XmlNamespaceDeclarationsAttribute allows you to store the prefixes, and the associated namespaces, used in an XML document. For example, one common usage of the attribute is to store XPath data, as it is defined by the World Wide Web Consortium ( document named "XML Language (XPath) Version 1.0". In brief, an XPath is a string containing many namespace prefixes and local names, along with some other syntax.

The XPath language allows for the association of a prefix with a path, and using the prefix within the XML document. For example, the following XML document named "select" contains a prefix ("cal") associated with a specific URI ( The element contains an attribute named "path" that contains the XPath.

<select xmlns:cal ="" path="cal:appointments/@startTime" />

The schema for this might be:

<element name="select">
         <attribute name="path" />

Without the XmlNamespaceDeclarationsAttribute, the association between the prefix and the namespace would be lost.

To retain the association between the prefix and the namespace URI, add a member that returns an XmlSerializerNamespaces object and apply the XmlNamespaceDeclarationsAttribute attribute to the member, as shown in the following C# and Visual Basic code:

// C#
public class Select {
  [XmlAttribute] public string path;
  [XmlNamespaceDeclarations] public XmlSerializerNamespaces xmlns;
' Visual Basic
Public Class Select
   <XmlAttribute> Public path As String
   <XmlNamespaceDeclarations> Public xmlns As XmlSerializerNamespaces
End Class

When serialized, the schema for the generated XML document will contain the XML Schema definition (XSD) element named appinfo. The element further contains a metadata element named keepNamespaceDeclarations, set to the name of the member that contains the namespace declarations. The schema for such an XML document is shown below:

<xs:element name="select">
         <xs:attribute name="path" />

On deserialization, the xmlns field will contain an XmlSerializerNamespaces object containing all namespace prefix definitions.

On serialization, the user can add prefix-namespace pairs to the XmlSerializerNamespaces object using the Add method. This is shown in the C# and Visual Basic code below:

// C#
using System;
using System.IO;
using System.Xml.Serialization;
public class Select {
   public string xpath;
   public XmlSerializerNamespaces xmlns;
public class Test {
   public static void Main(string[] args) {
      Select mySelect = new Select();
      mySelect.xpath = "myNS:ref/@common:y";
      mySelect.xmlns = new XmlSerializerNamespaces();
      mySelect.xmlns.Add("MyNS", "");
      mySelect.xmlns.Add("common", "");
      XmlSerializer ser = new XmlSerializer(typeof(Select));
      ser.Serialize(Console.Out, mySelect);
// Output:
// <?xml version="1.0" encoding="IBM437"?>
// <select xmlns:xsd="" xmlns:xsi="" 
// xmlns:common="" xmlns:MyNS="" xpath="myNS:ref/@common:y" />
' Visual Basic
Imports System
Imports System.IO
Imports System.Xml.Serialization
<XmlRoot("select")> _
Public Class SelectPath
   <XmlAttribute> _
   Public xpath As String 
   <XmlNamespaceDeclarations> _
   public xmlns As XmlSerializerNamespaces 
End Class
Public Class Test 
   Public Shared Sub Main() 
      Dim mySelect As SelectPath = New SelectPath()
      mySelect.xpath = "myNS:ref/@common:y"
      mySelect.xmlns = New XmlSerializerNamespaces()
      mySelect.xmlns.Add("MyNS", "")
      mySelect.xmlns.Add("common", "")
      Dim ser As XmlSerializer = New XmlSerializer(mySelect.GetType)
      ser.Serialize(Console.Out, mySelect)
   End Sub
End Class
' <?xml version="1.0" encoding="IBM437"?>
' <select xmlns:xsd="" xmlns:xsi="" 
' xmlns:common="" xmlns:MyNS="" xpath="myNS:ref/@common:y" />

Also note that the member to which the attribute is applied will contain only the prefix-namespace pairs belonging to the XML element defined by the class. For example, in the XML document shown below, only the prefix pair "cal" will be captured, but not the "x" prefix. To get that data, add a member with the XmlNamespaceDeclarationsAttribute to the class representing the root element.

<?xml version="1.0"?>
<x:root xmlns:x="">
  <x:select xmlns:cal="" path="cal:appointments/@cal:startTime" />


Namespace: System.Xml.Serialization

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

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

See Also

XmlNamespaceDeclarationsAttribute Members | System.Xml.Serialization Namespace