This documentation is archived and is not being maintained.

XmlElementAttribute Constructor (String)

.NET Framework 1.1

Initializes a new instance of the XmlElementAttribute class and specifies the name of the XML element.

[Visual Basic]
Public Sub New( _
   ByVal elementName As String _
)
[C#]
public XmlElementAttribute(
   string elementName
);
[C++]
public: XmlElementAttribute(
   String* elementName
);
[JScript]
public function XmlElementAttribute(
   elementName : String
);

Parameters

elementName
The XML element name of the serialized member.

Remarks

By default, the XmlSerializer uses the member name as the XML element name when serializing a class instance. For example, a field named Vehicle generates an XML element named Vehicle. However, if you need a different element, such as Cars, pass it in the elementName parameter.

Example

[Visual Basic, C#, C++] The following example shows a simple class that contains a single field named Vehicles. The example applies the XmlElementAttribute to the field and includes the elementName parameter, thereby instructing the XmlSerializer to generate XML elements named "Cars" rather than "Vehicles".

[Visual Basic] 
Public Class Transportation
    <XmlElement("Cars")> Public Vehicles As String
End Class


[C#] 
public class Transportation
{
   [XmlElement("Cars")]
   public string Vehicles;
}


[C++] 
public __gc class Transportation
{
public:
   [XmlElement(S"Cars")]
   String* Vehicles;
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlElementAttribute Class | XmlElementAttribute Members | System.Xml.Serialization Namespace | XmlElementAttribute Constructor Overload List

Show: