Export (0) Print
Expand All

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.

System.Object
  System.Attribute
    System.Xml.Serialization.XmlNamespaceDeclarationsAttribute

Namespace:  System.Xml.Serialization
Assemblies:   System.Xml (in System.Xml.dll)
  System.Xml.XmlSerializer (in System.Xml.XmlSerializer.dll)

[<AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple = false)>]
type XmlNamespaceDeclarationsAttribute =  
    class 
        inherit Attribute 
    end

The XmlNamespaceDeclarationsAttribute type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsXmlNamespaceDeclarationsAttributeInitializes a new instance of the XmlNamespaceDeclarationsAttribute class.
Top

  NameDescription
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
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 appsGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
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.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
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 appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

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 (www.w3.org) document named "XML Language (XPath) Version 1.0". In brief, an XPath is a string that contains 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 (http://www.cohowinery.com/calendar/). The element contains an attribute named "path" that contains the XPath.

 <select xmlns:cal ="http://www.cohowinery.com/calendar/" path="cal:appointments/@startTime" />

The schema for this might be:

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

Without the XmlNamespaceDeclarationsAttribute, the association between the prefix and the namespace is 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 contains 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 following XML fragment shows the schema:

 <xs:element name="select">
    <xs:complexType>
       <xs:annotation> 
          <xs:appinfo>
           <keepNamespaceDeclarations>xmlns</keepNamespaceDeclarations>
          </xs:appinfo> 
       </xs:annotation> 
       <xs:simpleContent>
          <xs:attribute name="path" />
       </xs:simpleContent>
    </xs:complexType>
 </xs:element>

On deserialization, the xmlns field contains an XmlSerializerNamespaces object that contains 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 following C# and Visual Basic code:

 // C#
 using System;
 using System.IO;
 using System.Xml.Serialization;
 [XmlRoot("select")]
 public class Select {
    [XmlAttribute]
    public string xpath;
    [XmlNamespaceDeclarations]
    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", "myNS.tempuri.org");
       mySelect.xmlns.Add("common", "common.tempuri.org");
       XmlSerializer ser = new XmlSerializer(typeof(Select));
       ser.Serialize(Console.Out, mySelect);
    }
 }
 // Output:
 // <?xml version="1.0" encoding="IBM437"?>
 // <select xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 // xmlns:common="common.tempuri.org" xmlns:MyNS="myNS.tempuri.org" 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", "myNS.tempuri.org")
       mySelect.xmlns.Add("common", "common.tempuri.org")
       Dim ser As XmlSerializer = New XmlSerializer(mySelect.GetType)
       ser.Serialize(Console.Out, mySelect)
    End Sub
 End Class
 'Output:
 ' <?xml version="1.0" encoding="IBM437"?>
 ' <select xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 ' xmlns:common="common.tempuri.org" xmlns:MyNS="myNS.tempuri.org" xpath="myNS:ref/@common:y" />

Also note that the member to which the attribute is applied contains only the prefix-namespace pairs that belong to the XML element defined by the class. For example, in the following XML document, only the prefix pair "cal" is captured, but not the "x" prefix. To get that data, add a member with the XmlNamespaceDeclarationsAttribute to the class that represents the root element.

 <?xml version="1.0"?>
 <x:root xmlns:x="http://www.cohowinery.com/x/">
   <x:select xmlns:cal="http://www.cohowinery.com/calendar/" path="cal:appointments/@cal:startTime" />
 </x:root>

.NET Framework

Supported in: 4.5.3, 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

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

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.
Show:
© 2014 Microsoft