This documentation is archived and is not being maintained.

XmlConvert Class

Encodes and decodes XML names and provides methods for converting between common language runtime types and XML Schema definition language (XSD) types. When converting data types the values returned are locale independent.

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

public ref class XmlConvert

Element and attribute names or ID values are limited to a range of XML characters according to the Extensible Markup Language (XML) 1.0 (Second Edition) recommendation, located at When names contain invalid characters, the EncodeName and DecodeName methods are used to translate them into valid XML names.

Many languages and applications such as Microsoft SQL Server and Microsoft Word, allow Unicode characters in their names, which are not valid in XML names. For example, if 'Order Detail' were a column heading in a database, the database allows the space between the words Order and Detail. However, in XML, the space between Order and Detail is considered an invalid XML character. Thus, the space, the invalid character, needs to be converted into an escaped hexadecimal encoding and can be decoded later.

The EncodeName method can be used with the XmlWriter class to ensure the names being written are valid XML names. The following C# code converts the name 'Order Detail' into a valid XML name and writes the element <Order_0x0020_Detail>My order</Order_0x0020_Detail>.

 writer.WriteElementString(XmlConvert.EncodeName("Order Detail"),"My order");

XmlConvert also provides methods that enable you to convert from a string to a.NET Framework data type and vice-versa. Locale settings are not taken into account during data conversion. The data types are based on the XML Schema (XSD) data types.


In the .NET Framework version 2.0, the XmlReader, XmlWriter, and XPathNavigator classes include new methods that can handle typed data. These new methods handle the string to .NET Framework data type conversion for you.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0