Anjo Gasa

While familiarizing myself with XML Serialization and controlling serialization using attributes, I've become confused whether there is a different between the use of "XmlElement" or "XmlElementAttribute" in actually writing the attribute in a class declaration, and in general for any serialization attribute.

For example, consider the following class:

public class Test
// public, parameterless constructor req'd for serialization
Test(void) {}


int m_Member1;

int m_Member2;

Even MSDN doesn't seem quite clear on which to use. From the code I've seen, it sounds like XmlElementAttribute is the object in the .NET framework that handles this, but I am to use "XmlElement" while actualing writing the attribute. In contrast, I have seen other code that appends the "Attribute" part of the object name. What is the difference, and which is correct


Re: XML and the .NET Framework XmlSerialization attributes: difference between [XmlElement(...)] and [XmlElementAttribute(...)]

Derek Smyth

Hi mate,

This is nothing to worry about. Attributes in .NET all follow a naming convention where attribute classes are appended with the word Attribute. So the actual name of the class is XmlElementAttribute, also the SerializableAttribute class, and so on. Basically all attribute class names end in Attribute and it's so common that .NET provides you with a shortcut that allows you to leave off the Attribute when you use it.

So XmlElement and XmlElementAttribute are exactly the same class. Your best using the shortcut (without Attribute) when you use the attribute but if you create your own custom attribute then name it with the Attribute suffix i.e. MyCustomFeatureAttribute.

Its also a bit confusing because XML uses the term attribute so you end up with an XmlAttributeAttribute class.

Thats why it's best to use the shortcut.