Export (0) Print
Expand All

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 bool Xmlns { get; set; }
/** @property */
public boolean get_Xmlns ()

/** @property */
public void set_Xmlns (boolean value)

public function get Xmlns () : boolean

public function set Xmlns (value : boolean)

Not applicable.

Property Value

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;
using System.IO;
using System.Xml;
using System.Xml.Serialization; 

public class Student
{
    [XmlAttributeAttribute]
    public string Name;

    [XmlNamespaceDeclarationsAttribute]
    public XmlSerializerNamespaces myNamespaces;

}
    
public class Run
{
    public static void Main()
    {
        Run test = new Run();
        test.SerializeStudent("Student.xml");
        test.DeserializeStudent("Student.xml");
    }

    public 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),"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("myns1", "http://www.cpandl.com");
        ns.Add("myns2", "http://www.cohowinery.com");
        myStudent.myNamespaces= ns;
        myStudent.Name= "Student1";

        FileStream fs = new FileStream(filename,FileMode.Create);

        xser.Serialize(fs,myStudent);
        fs.Close();

    }

    private 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),"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= (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("{0}:{1}", 
	    qNames[i].Name,qNames[i].Namespace);
        }
    }
}

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;
public class Student
{
    /** @attribute XmlAttributeAttribute()
     */
    public String name;

    /** @attribute XmlNamespaceDeclarationsAttribute()
     */
    public XmlSerializerNamespaces myNamespaces;
} //Student

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeStudent("Student.xml");
        test.DeserializeStudent("Student.xml");
    } //main

    public void SerializeStudent(String fileName)
    {
        XmlAttributes atts = new XmlAttributes();
        // Set to true to preserve namespaces, 
        // or false to ignore them.
        atts.set_Xmlns(true);

        XmlAttributeOverrides xOver = new XmlAttributeOverrides();
        // Add the XmlAttributes and specify the name of the element 
        // containing namespaces.
        xOver.Add(Student.class.ToType(), "myNamespaces", atts);
        // Create the XmlSerializer using the 
        // XmlAttributeOverrides object.
        XmlSerializer xSer = new XmlSerializer(Student.class.ToType(),
            xOver);

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

        FileStream fs = new FileStream(fileName, FileMode.Create);

        xSer.Serialize(fs, myStudent);
        fs.Close();
    } //SerializeStudent

    private void DeserializeStudent(String fileName)
    {
        XmlAttributes atts = new XmlAttributes();
        // Set to true to preserve namespaces, or false to ignore them.
        atts.set_Xmlns(true);

        XmlAttributeOverrides xOver = new XmlAttributeOverrides();
        // Add the XmlAttributes and specify the name of the 
        // element containing namespaces.
        xOver.Add(Student.class.ToType(), "myNamespaces", atts);
        // Create the XmlSerializer using the 
        // XmlAttributeOverrides object.
        XmlSerializer xSer = new XmlSerializer(Student.class.ToType(), xOver);

        FileStream fs = new FileStream(fileName, FileMode.Open);

        Student myStudent;
        myStudent = (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.get_Length(); i++) {
            Console.WriteLine("{0}:{1}", qNames[i].get_Name(), qNames[i].
                get_Namespace());
        }
    } //DeserializeStudent
} //Run

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft