Export (0) Print
Expand All

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.

For a list of all members of this type, see XmlConvert Members.

System.Object
   System.Xml.XmlConvert

[Visual Basic]
Public Class XmlConvert
[C#]
public class XmlConvert
[C++]
public __gc class XmlConvert
[JScript]
public class XmlConvert

Thread Safety

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

Remarks

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 www.w3.org/TR/2000/REC-xml-20001006.html. 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 XmlTextWriter 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. The table found at Data Type Support between XML Schema (XSD) Types and .NET Framework Types describes the mapping between XML Schema (XSD) and .NET data types.

In the following example, the XmlTextReader uses one of the XmlConvert.ToDouble method to read in data and convert it from a String to a Double.

Double price = XmlConvert.ToDouble(reader.Value);

Requirements

Namespace: System.Xml

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

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

See Also

XmlConvert Members | System.Xml Namespace

Show:
© 2014 Microsoft