Export (0) Print
Expand All
0 out of 95 rated this helpful - Rate this topic

XmlAnyElementAttributes.Add Method

Adds an XmlAnyElementAttribute to the collection.

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

public int Add (
	XmlAnyElementAttribute attribute
)
public int Add (
	XmlAnyElementAttribute attribute
)
public function Add (
	attribute : XmlAnyElementAttribute
) : int
Not applicable.

Parameters

attribute

The XmlAnyElementAttribute to add.

Return Value

The index of the newly added XmlAnyElementAttribute.

The following example creates a new XmlAnyElementAttribute and adds it to the collection of objects accessed through the XmlAnyElements property. The XmlAttributes is then added to an XmlAttributeOverrides, which is used to create an XmlSerializer. The XmlSerializer is used to serialize or deserialize an object. To see the effect of using the XmlAnyElementAttributes property, create an XML document named UnknownElements.xml by running the SerializeObject method in the Main method. Edit the resulting document to include other (unknown) elements. Comment out the SerializeObject call in the Main method, and uncomment the call to the DeserializeObject method, which prints out the name and value of any unknown XML element.

using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

public class Group{
   public string GroupName;
   [XmlAnyElement]
   public object[]Things;

}

public class Test{
   static void Main(){
      Test t = new Test();
      // 1 Run this and create the XML document.
      // 2 Add new elements to the XML document.
      // 3 Comment out the new line, and uncomment
      // the DeserializeObject line to deserialize the
      // XML document and see unknown elements.
      t.SerializeObject("UnknownElements.xml");
     
      // t.DeserializeObject("UnknownElements.xml");
   }

   private void SerializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof (Group));
      TextWriter writer = new StreamWriter(filename);
      Group g = new Group();
      g.GroupName = "MyGroup";
      ser.Serialize(writer, g);
      writer.Close();
   }

   
   private void DeserializeObject(string filename){

      XmlSerializer ser = CreateOverrideSerializer();
      // A FileStream is needed to read the XML document.
      FileStream fs = new FileStream(filename, FileMode.Open);
     Group g = (Group)
     	ser.Deserialize(fs);
     fs.Close();
     Console.WriteLine(g.GroupName);
     Console.WriteLine(g.Things.Length);
     foreach(XmlElement xelement in g.Things){
     Console.WriteLine(xelement.Name + ": " + xelement.InnerXml);
     }
   }

   private XmlSerializer CreateOverrideSerializer(){
      XmlAnyElementAttribute myAnyElement = 
      new XmlAnyElementAttribute();
      XmlAttributeOverrides xOverride = 
      new XmlAttributeOverrides();
      XmlAttributes xAtts = new XmlAttributes();
      xAtts.XmlAnyElements.Add(myAnyElement);
      xOverride.Add(typeof(Group), "Things", xAtts);
      return new XmlSerializer(typeof(Group) , xOverride);
   }
}

import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.*;
public class Group
{
    public String groupName;

    /** @attribute XmlAnyElement()
     */
    public Object things[];
} //Group

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        // 1 Run this and create the XML document.
        // 2 Add new elements to the XML document.
        // 3 Comment out the new line, and uncomment
        // the DeserializeObject line to deserialize the
        // XML document and see unknown elements.
        t.SerializeObject("UnknownElements.xml");
    } //main

    // t.DeserializeObject("UnknownElements.xml");
    private void SerializeObject(String fileName)
    {
        XmlSerializer ser = new XmlSerializer(Group.class.ToType());
        TextWriter writer = new StreamWriter(fileName);
        Group g = new Group();
        g.groupName = "MyGroup";
        ser.Serialize(writer, g);
        writer.Close();
    } //SerializeObject

    private void DeserializeObject(String fileName)
    {
        XmlSerializer ser = CreateOverrideSerializer();
        // A FileStream is needed to read the XML document.
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Group g = (Group)ser.Deserialize(fs);
        fs.Close();
        Console.WriteLine(g.groupName);
        Console.WriteLine(g.things.get_Length());
        for (int iCtr = 0; iCtr < g.things.get_Count(); iCtr++) {
            XmlElement xElement = (XmlElement)g.things[iCtr];
            Console.WriteLine(xElement.get_Name() + ": " 
                + xElement.get_InnerXml());
        }
    } //DeserializeObject

    private XmlSerializer CreateOverrideSerializer()
    {
        XmlAnyElementAttribute myAnyElement = new XmlAnyElementAttribute();
        XmlAttributeOverrides xOverride = new XmlAttributeOverrides();
        XmlAttributes xAtts = new XmlAttributes();
        xAtts.get_XmlAnyElements().Add(myAnyElement);
        xOverride.Add(Group.class.ToType(), "things", xAtts);
        return new XmlSerializer(Group.class.ToType(), xOverride);
    } //CreateOverrideSerializer
} //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

XNA Framework

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

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.