UnknownAttribute Event
Collapse the table of content
Expand the table of content

XmlSerializer.UnknownAttribute Event


Occurs when the XmlSerializer encounters an XML attribute of unknown type during deserialization.

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

public event XmlAttributeEventHandler UnknownAttribute

By default, after calling the Deserialize method, the XmlSerializer ignores XML attributes of unknown types. However, you can use this event to handle such node types.

If the instance of the class being deserialized contains a field that returns an array of XmlAttribute objects and an XmlAnyAttributeAttribute has been applied to the field, the UnknownAttribute event does not occur. Instead, all unknown XML attributes are collected into the array.

The following example prints information about any unknown attributes encountered while deserializing an XML document.

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

public class Group{
   public string GroupName;

public class Test{
   static void Main(){
      Test t = new Test();
      // Deserialize the file containing unknown elements.
   private void Serializer_UnknownAttribute(object sender, XmlAttributeEventArgs e){
      Console.WriteLine("Unknown Attribute");
      Console.WriteLine("\t" + e.Attr.Name + " " + e.Attr.InnerXml);
      Console.WriteLine("\t LineNumber: " + e.LineNumber);
      Console.WriteLine("\t LinePosition: " + e.LinePosition);

      Group x  = (Group) e.ObjectBeingDeserialized;
      Console.WriteLine (x.GroupName);
      Console.WriteLine (sender.ToString());
   private void DeserializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      // Add a delegate to handle unknown element events.
      ser.UnknownAttribute+=new XmlAttributeEventHandler(Serializer_UnknownAttribute);
      // A FileStream is needed to read the XML document.
     FileStream fs = new FileStream(filename, FileMode.Open);
     Group g = (Group) ser.Deserialize(fs);

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft