Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
XmlConvert Class
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
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)
Visual Basic (Declaration)
Public Class XmlConvert
Visual Basic (Usage)
Dim instance As XmlConvert
C#
public class XmlConvert
Visual C++
public ref class XmlConvert
F#
type XmlConvert =  class end

Element and attribute names or ID values are limited to a range of XML characters according to the Extensible Markup Language (XML) 1.0 spec (Fourth Edition) recommendation. 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.

NoteNote

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.

System..::.Object
  System.Xml..::.XmlConvert
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, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, 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: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4

XNA Framework

Supported in: 3.0, 2.0, 1.0
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker