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.
Assembly: System.Xml (in System.Xml.dll)
The XmlConvert type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() ![]() ![]() | DecodeName | Decodes a name. This method does the reverse of the EncodeName and EncodeLocalName methods. |
![]() ![]() ![]() ![]() | EncodeLocalName | Converts the name to a valid XML local name. |
![]() ![]() ![]() ![]() | EncodeName | Converts the name to a valid XML name. |
![]() ![]() ![]() ![]() | EncodeNmToken | Verifies the name is valid according to the XML specification. |
![]() ![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() ![]() | FromBinHexString | Obsolete. Converts a BinHex-encoded string to a byte array. |
![]() ![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() ![]() ![]() | IsNCNameChar | Checks whether the passed-in character is a valid non-colon character type. |
![]() ![]() | IsPublicIdChar | Returns the passed-in character instance if the character in the argument is a valid public id character, otherwise nullptr. |
![]() ![]() ![]() | IsStartNCNameChar | Checks if the passed-in character is a valid Start Name Character type. |
![]() ![]() | IsWhitespaceChar | Checks if the passed-in character is a valid XML whitespace character. |
![]() ![]() | IsXmlChar | Checks if the passed-in character is a valid XML character. |
![]() ![]() | IsXmlSurrogatePair | Checks if the passed-in surrogate pair of characters is a valid XML character. |
![]() ![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() ![]() ![]() | ToBinHexString | Obsolete. Converts a byte array to a BinHex-encoded string. |
![]() ![]() ![]() ![]() | ToBoolean | Converts the String to a Boolean equivalent. |
![]() ![]() ![]() ![]() | ToByte | Converts the String to a Byte equivalent. |
![]() ![]() ![]() ![]() | ToChar | Converts the String to a Char equivalent. |
![]() ![]() ![]() | ToDateTime(String) | Obsolete. Converts the String to a DateTime equivalent. |
![]() ![]() ![]() | ToDateTime(String, String) | Converts the String to a DateTime equivalent. |
![]() ![]() ![]() | ToDateTime(String, array<String>) | Converts the String to a DateTime equivalent. |
![]() ![]() ![]() | ToDateTime(String, XmlDateTimeSerializationMode) | Converts the String to a DateTime using the XmlDateTimeSerializationMode specified |
![]() ![]() ![]() | ToDateTimeOffset(String) | Converts the supplied String to a DateTimeOffset equivalent. |
![]() ![]() ![]() | ToDateTimeOffset(String, String) | Converts the supplied String to a DateTimeOffset equivalent. |
![]() ![]() ![]() | ToDateTimeOffset(String, array<String>) | Converts the supplied String to a DateTimeOffset equivalent. |
![]() ![]() ![]() ![]() | ToDecimal | Converts the String to a Decimal equivalent. |
![]() ![]() ![]() ![]() | ToDouble | Converts the String to a Double equivalent. |
![]() ![]() ![]() ![]() | ToGuid | Converts the String to a Guid equivalent. |
![]() ![]() ![]() ![]() | ToInt16 | Converts the String to a Int16 equivalent. |
![]() ![]() ![]() ![]() | ToInt32 | Converts the String to a Int32 equivalent. |
![]() ![]() ![]() ![]() | ToInt64 | Converts the String to a Int64 equivalent. |
![]() ![]() ![]() ![]() | ToSByte | Converts the String to a SByte equivalent. |
![]() ![]() ![]() ![]() | ToSingle | Converts the String to a Single equivalent. |
![]() ![]() | ToString() | Returns a string that represents the current object. (Inherited from Object.) |
![]() ![]() ![]() ![]() | ToString(Boolean) | Converts the Boolean to a String. |
![]() ![]() ![]() | ToString(Byte) | Converts the Byte to a String. |
![]() ![]() ![]() ![]() | ToString(Char) | Converts the Char to a String. |
![]() ![]() ![]() | ToString(DateTime) | Obsolete. Converts the DateTime to a String. |
![]() ![]() ![]() | ToString(DateTimeOffset) | Converts the supplied DateTimeOffset to a String. |
![]() ![]() ![]() ![]() | ToString(Decimal) | Converts the Decimal to a String. |
![]() ![]() ![]() ![]() | ToString(Double) | Converts the Double to a String. |
![]() ![]() ![]() ![]() | ToString(Guid) | Converts the Guid to a String. |
![]() ![]() ![]() ![]() | ToString(Int16) | Converts the Int16 to a String. |
![]() ![]() ![]() ![]() | ToString(Int32) | Converts the Int32 to a String. |
![]() ![]() ![]() ![]() | ToString(Int64) | Converts the Int64 to a String. |
![]() ![]() ![]() ![]() | ToString(SByte) | Converts the SByte to a String. |
![]() ![]() ![]() ![]() | ToString(Single) | Converts the Single to a String. |
![]() ![]() ![]() ![]() | ToString(TimeSpan) | Converts the TimeSpan to a String. |
![]() ![]() ![]() | ToString(UInt16) | Converts the UInt16 to a String. |
![]() ![]() ![]() ![]() | ToString(UInt32) | Converts the UInt32 to a String. |
![]() ![]() ![]() ![]() | ToString(UInt64) | Converts the UInt64 to a String. |
![]() ![]() ![]() | ToString(DateTime, String) | Converts the DateTime to a String. |
![]() ![]() ![]() | ToString(DateTime, XmlDateTimeSerializationMode) | Converts the DateTime to a String using the XmlDateTimeSerializationMode specified. |
![]() ![]() ![]() | ToString(DateTimeOffset, String) | Converts the supplied DateTimeOffset to a String in the specified format. |
![]() ![]() ![]() ![]() | ToTimeSpan | Converts the String to a TimeSpan equivalent. |
![]() ![]() ![]() ![]() | ToUInt16 | Converts the String to a UInt16 equivalent. |
![]() ![]() ![]() ![]() | ToUInt32 | Converts the String to a UInt32 equivalent. |
![]() ![]() ![]() ![]() | ToUInt64 | Converts the String to a UInt64 equivalent. |
![]() ![]() ![]() ![]() | VerifyName | Verifies that the name is a valid name according to the W3C Extended Markup Language recommendation. |
![]() ![]() ![]() ![]() | VerifyNCName | Verifies that the name is a valid NCName according to the W3C Extended Markup Language recommendation. An NCName is a name that cannot contain a colon. |
![]() ![]() ![]() ![]() | VerifyNMTOKEN | Verifies that the string is a valid NMTOKEN according to the W3C XML Schema Part2: Datatypes recommendation |
![]() ![]() ![]() | VerifyPublicId | Returns the passed in string instance if all the characters in the string argument are valid public id characters. |
![]() ![]() ![]() | VerifyTOKEN | Verifies that the string is a valid token according to the W3C XML Schema Part2: Datatypes recommendation. |
![]() ![]() ![]() | VerifyWhitespace | Returns the passed-in string instance if all the characters in the string argument are valid whitespace characters. |
![]() ![]() ![]() | VerifyXmlChars | Returns the passed-in string if all the characters and surrogate pair characters in the string argument are valid XML characters, otherwise nullptr. |
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.
Note |
|---|
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. |
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

