Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XmlAttributes::Xmlns Property

Gets or sets a value that specifies whether to keep all namespace declarations when an object containing a member that returns an XmlSerializerNamespaces object is overridden.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)
public:
property bool Xmlns {
	bool get ();
	void set (bool value);
}

Property Value

Type: System::Boolean
true if the namespace declarations should be kept; otherwise, false.

The following example contains a class named Student. The class contains a member named MyNamespaces that returns an XmlSerializerNamespaces object. The example creates an XmlAttributes object that is added to an instance of the XmlAttributeOverrides class. The Xmlns property is set to true, which instructs the XmlSerializer to preserve the namespaces when the serialization of the Student object is overridden.

#using <System.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public ref class Student
{
public:

   [XmlAttributeAttribute]
   String^ Name;

   [XmlNamespaceDeclarationsAttribute]
   XmlSerializerNamespaces^ myNamespaces;
};

void SerializeStudent( String^ filename );
void DeserializeStudent( String^ filename );
int main()
{
   SerializeStudent( "Student.xml" );
   DeserializeStudent( "Student.xml" );
}

void SerializeStudent( String^ filename )
{
   XmlAttributes^ atts = gcnew XmlAttributes;

   // Set to true to preserve namespaces,  
   // or false to ignore them.
   atts->Xmlns = true;
   XmlAttributeOverrides^ xover = gcnew XmlAttributeOverrides;

   // Add the XmlAttributes and specify the name of the element  
   // containing namespaces.
   xover->Add( Student::typeid, "myNamespaces", atts );

   // Create the XmlSerializer using the  
   // XmlAttributeOverrides object.
   XmlSerializer^ xser = gcnew XmlSerializer( Student::typeid,xover );
   Student^ myStudent = gcnew Student;
   XmlSerializerNamespaces^ ns = gcnew XmlSerializerNamespaces;
   ns->Add( "myns1", "http://www.cpandl.com" );
   ns->Add( "myns2", "http://www.cohowinery.com" );
   myStudent->myNamespaces = ns;
   myStudent->Name = "Student1";
   FileStream^ fs = gcnew FileStream( filename,FileMode::Create );
   xser->Serialize( fs, myStudent );
   fs->Close();
}

void DeserializeStudent( String^ filename )
{
   XmlAttributes^ atts = gcnew XmlAttributes;

   // Set to true to preserve namespaces, or false to ignore them.
   atts->Xmlns = true;
   XmlAttributeOverrides^ xover = gcnew XmlAttributeOverrides;

   // Add the XmlAttributes and specify the name of the  
   // element containing namespaces.
   xover->Add( Student::typeid, "myNamespaces", atts );

   // Create the XmlSerializer using the  
   // XmlAttributeOverrides object.
   XmlSerializer^ xser = gcnew XmlSerializer( Student::typeid,xover );
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Student^ myStudent;
   myStudent = safe_cast<Student^>(xser->Deserialize( fs ));
   fs->Close();

   // Use the ToArray method to get an array of  
   // XmlQualifiedName objects. 
   array<XmlQualifiedName^>^qNames = myStudent->myNamespaces->ToArray();
   for ( int i = 0; i < qNames->Length; i++ )
   {
      Console::WriteLine( "{0}:{1}", qNames[ i ]->Name, qNames[ i ]->Namespace );

   }
}
#using <mscorlib.dll>
#using <System.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization; 

public __gc class Student
{
public:
   [XmlAttributeAttribute]
   String* Name;

   [XmlNamespaceDeclarationsAttribute]
   XmlSerializerNamespaces* myNamespaces;

};

void SerializeStudent(String* filename);
void DeserializeStudent(String* filename);

int main()
{
   SerializeStudent(S"Student.xml");
   DeserializeStudent(S"Student.xml");
}

void SerializeStudent(String* filename)
{
   XmlAttributes* atts = new XmlAttributes();
   // Set to true to preserve namespaces, 
   // or false to ignore them.
   atts->Xmlns=true;

   XmlAttributeOverrides* xover = new XmlAttributeOverrides();
   // Add the XmlAttributes and specify the name of the element 
   // containing namespaces.
   xover->Add(__typeof(Student),S"myNamespaces", atts);
   // Create the XmlSerializer using the 
   // XmlAttributeOverrides object.
   XmlSerializer* xser = new XmlSerializer(__typeof (Student),xover);

   Student* myStudent = new Student();
   XmlSerializerNamespaces* ns = new XmlSerializerNamespaces();
   ns->Add(S"myns1", S"http://www.cpandl.com");
   ns->Add(S"myns2", S"http://www.cohowinery.com");
   myStudent->myNamespaces= ns;
   myStudent->Name= S"Student1";

   FileStream* fs = new FileStream(filename,FileMode::Create);

   xser->Serialize(fs,myStudent);
   fs->Close();
}

void DeserializeStudent(String* filename)
{
   XmlAttributes* atts = new XmlAttributes();
   // Set to true to preserve namespaces, or false to ignore them.
   atts->Xmlns=true;

   XmlAttributeOverrides* xover = new XmlAttributeOverrides();
   // Add the XmlAttributes and specify the name of the 
   // element containing namespaces.
   xover->Add(__typeof(Student),S"myNamespaces", atts);

   // Create the XmlSerializer using the 
   // XmlAttributeOverrides object.
   XmlSerializer* xser = 
      new XmlSerializer(__typeof (Student),xover);

   FileStream* fs = new FileStream(filename,FileMode::Open);

   Student* myStudent;
   myStudent= __try_cast<Student*>( xser->Deserialize( fs ));
   fs->Close();

   // Use the ToArray method to get an array of 
   // XmlQualifiedName objects.
   XmlQualifiedName* qNames[]= myStudent->myNamespaces->ToArray();
   for(int i = 0; i < qNames->Length; i++)
   {
      Console::WriteLine( S"{0}:{1}", qNames[i]->Name, qNames[i]->Namespace );
   }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.