Attr Property

XmlAttributeEventArgs.Attr Property


Gets an object that represents the unknown XML attribute.

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

public XmlAttribute Attr { get; }

Property Value

Type: System.Xml.XmlAttribute

An XmlAttribute that represents the unknown XML attribute.

The following example deserializes a class named Group from a file named UnknownAttributes.xml. Whenever an element is found in the file that has no corresponding member in the class, the UnknownAttribute event occurs. To try the example, paste the following XML code into a file named UnknownAttributes.xml.

<?xml version="1.0" encoding="utf-8"?>
<Group xmlns:xsi="" xmlns:xsd="" GroupType = 'Technical' GroupNumber = '42' GroupBase = 'Red'>
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