This documentation is archived and is not being maintained.

XmlAnyElementAttribute.Name Property

Gets or sets the XML element name.

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

public string Name { get; set; }

Property Value

Type: System.String
The name of the XML element.


The element name of an array member does not match the element name specified by the Name property.

If you specify a Name property value when applying the attribute, all XmlElement or XmlNode objects inserted into the array must have the same element name and default namespace, or an exception is thrown. If you set the Namespace property value, you must set the Name property as well, and the XmlElement or XmlNode objects must also have the same name and namespace values. If no Name value is specified, the XmlElement or XmlNode objects can have any element name.

When you call the Deserialize method of the XmlSerializer class, all attributes that do not have a corresponding member in the object being deserialized are collected in the array. If you specify a Name value, the array contains only XML elements with that name. If you do not specify a Name value, the array contains all elements that have no corresponding member in the class. If a class contains more than one field to which the attribute is applied, use the Name and Namespace properties to differentiate between the contents of the arrays. If such a class (with multiple fields) also contains one field that has no differentiating property values set (that is, Name and Namespace) during deserialization, the array contains any XML elements that are not already contained in the other arrays. If you add more than one field that does not have a differentiating Name or Namespace value set, the last field in the class contains all unknown elements that are not already contained in the other arrays, and any other fields are set to null.

You can apply multiple instances of the XmlAnyElementAttribute to a class member, but each instance must have a distinct Name property value. Or, if the same Name property is set for each instance, a distinct Namespace property value must be set for each instance.

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

[XmlRoot(Namespace = "")]
public class Group{
   public string GroupName;

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

   // This is for serializing City elements.   
   (Name = "City", 
   Namespace = "")]
   public XmlElement[] UnknownCity;

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

public class Test{
   static void Main(){
      Test t = new Test();

   private void SerializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      // Create an XmlNamespaces to use.
      XmlSerializerNamespaces namespaces =
      new XmlSerializerNamespaces();
      namespaces.Add("c", "");
      namespaces.Add("i", "");
      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", "");
      El1.InnerText = "John";
      XmlElement El2 = xDoc.CreateElement("Employee", "");
      El2.InnerText = "Joan";
      XmlElement El3 = xDoc.CreateElement("Employee", "");
      El3.InnerText = "Jim";
      myGroup.UnknownEmployees= new XmlElement[]{El1, El2, El3};     

      // Create an array of City XmlElement objects.
      XmlElement inf1 = xDoc.CreateElement("City", "");
      inf1.InnerText = "Tokyo";
      XmlElement inf2 = xDoc.CreateElement("City", "");     
      inf2.InnerText = "New York";
      XmlElement inf3 = xDoc.CreateElement("City", "");     
      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);


   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);
      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);}

.NET Framework

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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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