XmlAnyElementAttribute.Namespace Property

Gets or sets the XML namespace generated in the XML document.

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

public string Namespace { get; set; }
/** @property */
public String get_Namespace ()

/** @property */
public void set_Namespace (String value)

public function get Namespace () : String

public function set Namespace (value : String)

Not applicable.

Property Value

An XML namespace.

The Namespace property allows you to set a unique name for the XML attribute. The Namespace property conforms to the rules for creating an XML namespace as found in the World Wide Web Consortium (www.w3.org) document, "Namespaces in XML."

To set the Namespace property to a prefixed name, create an XmlSerializerNamespaces that contains the namespaces and prefixes used in the XML document. Set the Namespace property to one of the namespaces in the XmlSerializerNamespaces. When the XML is generated, the attribute name is correctly prefixed with the prefix associated with the specified namespace.

using System;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;


[XmlRoot(Namespace = "http://www.cohowinery.com")]
public class Group{
   public string GroupName;

   // This is for serializing Employee elements.
   [XmlAnyElement(Name = "Employee")]
   public XmlElement[] UnknownEmployees;

   // This is for serializing City elements.   
   [XmlAnyElement
   (Name = "City", 
   Namespace = "http://www.cpandl.com")]
   public XmlElement[] UnknownCity;

    // This one is for all other unknown elements.
   [XmlAnyElement]
   public XmlElement[] UnknownElements;
}

public class Test{
   static void Main(){
      Test t = new Test();
      t.SerializeObject("AnyElementArray.xml");
      t.DeserializeObject("AnyElementArray.xml");
      Console.WriteLine("Done");
   }

   private void SerializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      // Create an XmlNamespaces to use.
      XmlSerializerNamespaces namespaces =
      new XmlSerializerNamespaces();
      namespaces.Add("c", "http://www.cohowinery.com");
      namespaces.Add("i", "http://www.cpandl.com");
      Group myGroup = new Group();
      // Create arrays of arbitrary XmlElement objects.
      // First create an XmlDocument, used to create the 
      // XmlElement objects.
      XmlDocument xDoc = new XmlDocument();

      // Create an array of Employee XmlElement objects.
      XmlElement El1 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El1.InnerText = "John";
      XmlElement El2 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El2.InnerText = "Joan";
      XmlElement El3 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El3.InnerText = "Jim";
      myGroup.UnknownEmployees= new XmlElement[]{El1, El2, El3};     
    
      // Create an array of City XmlElement objects.
      XmlElement inf1 = xDoc.CreateElement("City", "http://www.cpandl.com");
      inf1.InnerText = "Tokyo";
      XmlElement inf2 = xDoc.CreateElement("City", "http://www.cpandl.com");     
      inf2.InnerText = "New York";
      XmlElement inf3 = xDoc.CreateElement("City", "http://www.cpandl.com");     
      inf3.InnerText = "Rome";

      myGroup.UnknownCity = new XmlElement[]{inf1, inf2, inf3};

      XmlElement xEl1 = xDoc.CreateElement("bld");
      xEl1.InnerText = "42";
      XmlElement xEl2 = xDoc.CreateElement("Region");
      xEl2.InnerText = "West";
      XmlElement xEl3 = xDoc.CreateElement("type");
      xEl3.InnerText = "Technical";
      myGroup.UnknownElements = 
      	new XmlElement[]{xEl1,xEl2,xEl3};
      // Serialize the class, and close the TextWriter.
      TextWriter writer = new StreamWriter(filename);
      ser.Serialize(writer, myGroup, namespaces);
      writer.Close();

   }

   private void DeserializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup;
      myGroup = (Group)ser.Deserialize(fs);
      fs.Close();
      foreach(XmlElement xEmp in myGroup.UnknownEmployees){
         Console.WriteLine(xEmp.LocalName + ": " + xEmp.InnerText);}
      foreach(XmlElement xCity in myGroup.UnknownCity){
         Console.WriteLine(xCity.LocalName + ": " + xCity.InnerText);}
      foreach(XmlElement xEl in myGroup.UnknownElements){
         Console.WriteLine(xEl.LocalName + ": " + xEl.InnerText);}
   }
 }


import System.*;
import System.Text.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.*;
import System.Xml.Schema.*;
/** @attribute XmlRoot(Namespace = "http://www.cohowinery.com")
 */
public class Group
{
    public String groupName;

    // This is for serializing Employee elements.
    /** @attribute XmlAnyElement(Name = "Employee")
     */
    public XmlElement unknownEmployees[];

    // This is for serializing City elements.   
    /** @attribute XmlAnyElement(Name = "City", Namespace =
        "http://www.cpandl.com")
     */
    public XmlElement unknownCity[];

    // This one is for all other unknown elements.
    /** @attribute XmlAnyElement()
     */
    public XmlElement unknownElements[];
} //Group

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.SerializeObject("AnyElementArray.xml");
        t.DeserializeObject("AnyElementArray.xml");
        Console.WriteLine("Done");
    } //main

    private void SerializeObject(String fileName)
    {
        XmlSerializer ser = new XmlSerializer(Group.class.ToType());
        // Create an XmlNamespaces to use.
        XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
        namespaces.Add("c", "http://www.cohowinery.com");
        namespaces.Add("i", "http://www.cpandl.com");
        Group myGroup = new Group();
        // Create arrays of arbitrary XmlElement objects.
        // First create an XmlDocument, used to create the 
        // XmlElement objects.
        XmlDocument xDoc = new XmlDocument();
        // Create an array of Employee XmlElement objects.
        XmlElement el1 = xDoc.CreateElement("Employee",
                "http://www.cohowinery.com");
        el1.set_InnerText("John");
        XmlElement el2 = xDoc.CreateElement("Employee", 
                "http://www.cohowinery.com");
        el2.set_InnerText("Joan");
        XmlElement el3 = xDoc.CreateElement("Employee", 
                "http://www.cohowinery.com");
        el3.set_InnerText("Jim");
        myGroup.unknownEmployees = new XmlElement[] { el1, el2, el3 };
        // Create an array of City XmlElement objects.
        XmlElement inf1 = xDoc.CreateElement("City", "http://www.cpandl.com");
        inf1.set_InnerText("Tokyo");
        XmlElement inf2 = xDoc.CreateElement("City", "http://www.cpandl.com");
        inf2.set_InnerText("New York");
        XmlElement inf3 = xDoc.CreateElement("City", "http://www.cpandl.com");
        inf3.set_InnerText("Rome");

        myGroup.unknownCity = new XmlElement[] { inf1, inf2, inf3 };

        XmlElement xEl1 = xDoc.CreateElement("bld");
        xEl1.set_InnerText("42");
        XmlElement xEl2 = xDoc.CreateElement("Region");
        xEl2.set_InnerText("West");
        XmlElement xEl3 = xDoc.CreateElement("type");
        xEl3.set_InnerText("Technical");
        myGroup.unknownElements = new XmlElement[] { xEl1, xEl2, xEl3 };
        // Serialize the class, and close the TextWriter.
        TextWriter writer = new StreamWriter(fileName);
        ser.Serialize(writer, myGroup, namespaces);
        writer.Close();
    } //SerializeObject

    private void DeserializeObject(String fileName)
    {
        XmlSerializer ser = new XmlSerializer(Group.class.ToType());
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Group myGroup;
        myGroup = (Group)ser.Deserialize(fs);
        fs.Close();
        for (int iCtr = 0;iCtr < myGroup.unknownEmployees.get_Count();iCtr++) {
            XmlElement xEmp = (XmlElement)myGroup.unknownEmployees.
                get_Item(iCtr);
            Console.WriteLine(xEmp.get_LocalName() + ": " 
                + xEmp.get_InnerText());
        }
        for (int iCtr = 0; iCtr < myGroup.unknownCity.get_Count(); iCtr++) {
            XmlElement xCity = (XmlElement)myGroup.unknownCity.get_Item(iCtr);
            Console.WriteLine(xCity.get_LocalName() + ": " 
                + xCity.get_InnerText());
        }
        for (int iCtr = 0;iCtr < myGroup.unknownElements.get_Count();iCtr++) {
            XmlElement xEl = (XmlElement)myGroup.unknownElements.get_Item(iCtr);
            Console.WriteLine(xEl.get_LocalName() + ": " + xEl.get_InnerText());
        }
    } //DeserializeObject
} //Test 

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, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show: