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)

System.Object
  System.Xml.XmlConvert

public class XmlConvert

NameDescription
System_CAPS_pubmethodXmlConvert()

Initializes a new instance of the XmlConvert class.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticDecodeName(String)

Decodes a name. This method does the reverse of the EncodeName and EncodeLocalName methods.

System_CAPS_pubmethodSystem_CAPS_staticEncodeLocalName(String)

Converts the name to a valid XML local name.

System_CAPS_pubmethodSystem_CAPS_staticEncodeName(String)

Converts the name to a valid XML name.

System_CAPS_pubmethodSystem_CAPS_staticEncodeNmToken(String)

Verifies the name is valid according to the XML specification.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticIsNCNameChar(Char)

Checks whether the passed-in character is a valid non-colon character type.

System_CAPS_pubmethodSystem_CAPS_staticIsPublicIdChar(Char)

Returns the passed-in character instance if the character in the argument is a valid public id character, otherwise null.

System_CAPS_pubmethodSystem_CAPS_staticIsStartNCNameChar(Char)

Checks if the passed-in character is a valid Start Name Character type.

System_CAPS_pubmethodSystem_CAPS_staticIsWhitespaceChar(Char)

Checks if the passed-in character is a valid XML whitespace character.

System_CAPS_pubmethodSystem_CAPS_staticIsXmlChar(Char)

Checks if the passed-in character is a valid XML character.

System_CAPS_pubmethodSystem_CAPS_staticIsXmlSurrogatePair(Char, Char)

Checks if the passed-in surrogate pair of characters is a valid XML character.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticToBoolean(String)

Converts the String to a Boolean equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToByte(String)

Converts the String to a Byte equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToChar(String)

Converts the String to a Char equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToDateTime(String)

Obsolete.Converts the String to a DateTime equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToDateTime(String, String)

Converts the String to a DateTime equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToDateTime(String, String[])

Converts the String to a DateTime equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToDateTime(String, XmlDateTimeSerializationMode)

Converts the String to a DateTime using the XmlDateTimeSerializationMode specified

System_CAPS_pubmethodSystem_CAPS_staticToDateTimeOffset(String)

Converts the supplied String to a DateTimeOffset equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToDateTimeOffset(String, String)

Converts the supplied String to a DateTimeOffset equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToDateTimeOffset(String, String[])

Converts the supplied String to a DateTimeOffset equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToDecimal(String)

Converts the String to a Decimal equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToDouble(String)

Converts the String to a Double equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToGuid(String)

Converts the String to a Guid equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToInt16(String)

Converts the String to a Int16 equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToInt32(String)

Converts the String to a Int32 equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToInt64(String)

Converts the String to a Int64 equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToSByte(String)

Converts the String to a SByte equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToSingle(String)

Converts the String to a Single equivalent.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticToString(Boolean)

Converts the Boolean to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(Byte)

Converts the Byte to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(Char)

Converts the Char to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(DateTime)

Obsolete.Converts the DateTime to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(DateTime, String)

Converts the DateTime to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(DateTime, XmlDateTimeSerializationMode)

Converts the DateTime to a String using the XmlDateTimeSerializationMode specified.

System_CAPS_pubmethodSystem_CAPS_staticToString(DateTimeOffset)

Converts the supplied DateTimeOffset to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(DateTimeOffset, String)

Converts the supplied DateTimeOffset to a String in the specified format.

System_CAPS_pubmethodSystem_CAPS_staticToString(Decimal)

Converts the Decimal to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(Double)

Converts the Double to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(Guid)

Converts the Guid to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(Int16)

Converts the Int16 to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(Int32)

Converts the Int32 to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(Int64)

Converts the Int64 to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(SByte)

Converts the SByte to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(Single)

Converts the Single to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(TimeSpan)

Converts the TimeSpan to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(UInt16)

Converts the UInt16 to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(UInt32)

Converts the UInt32 to a String.

System_CAPS_pubmethodSystem_CAPS_staticToString(UInt64)

Converts the UInt64 to a String.

System_CAPS_pubmethodSystem_CAPS_staticToTimeSpan(String)

Converts the String to a TimeSpan equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToUInt16(String)

Converts the String to a UInt16 equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToUInt32(String)

Converts the String to a UInt32 equivalent.

System_CAPS_pubmethodSystem_CAPS_staticToUInt64(String)

Converts the String to a UInt64 equivalent.

System_CAPS_pubmethodSystem_CAPS_staticVerifyName(String)

Verifies that the name is a valid name according to the W3C Extended Markup Language recommendation.

System_CAPS_pubmethodSystem_CAPS_staticVerifyNCName(String)

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.

System_CAPS_pubmethodSystem_CAPS_staticVerifyNMTOKEN(String)

Verifies that the string is a valid NMTOKEN according to the W3C XML Schema Part2: Datatypes recommendation

System_CAPS_pubmethodSystem_CAPS_staticVerifyPublicId(String)

Returns the passed in string instance if all the characters in the string argument are valid public id characters.

System_CAPS_pubmethodSystem_CAPS_staticVerifyTOKEN(String)

Verifies that the string is a valid token according to the W3C XML Schema Part2: Datatypes recommendation.

System_CAPS_pubmethodSystem_CAPS_staticVerifyWhitespace(String)

Returns the passed-in string instance if all the characters in the string argument are valid whitespace characters.

System_CAPS_pubmethodSystem_CAPS_staticVerifyXmlChars(String)

Returns the passed-in string if all the characters and surrogate pair characters in the string argument are valid XML characters, otherwise an XmlException is thrown with information on the first invalid character encountered.

The XmlConvert class is functionally equivalent to the Convert class, but it supports XML standards. The type system is based on the XML Schema definition language (XSD) schema type, and the values returned are always locale-independent.

Element and attribute names or ID values are limited to a range of XML characters according to the W3C XML 1.0 recommendation. When names contain invalid characters, you can use the EncodeName and DecodeName methods in this class to translate them into valid XML names.

For example, if you want to use the column heading "Order Detail" in a database, the database allows the space between the two words. However, in XML, the space between "Order" and "Detail" is considered an invalid XML character. You have to convert it into an escaped hexadecimal encoding and decode it later.

You can use the EncodeName method 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");

The following XmlConvert methods perform encoding and decoding.

Method

Description

EncodeName

Takes a name and returns the encoded name along with any invalid character that is replaced by an escape string. This method allows colons in any position, which means that the name may still be invalid according to the W3C Namespaces in XML 1.0 recommendation.

EncodeNmToken

Takes a name and returns the encoded name.

EncodeLocalName

Same as EncodeName except that it also encodes the colon character, guaranteeing that the name can be used as the LocalName part of a namespace-qualified name.

DecodeName

Reverses the transformation for all the encoding methods.

The XmlConvert class contains two methods that check the characters in an element or attribute name and verify that the name conforms to the rules set by the W3C XML 1.0 recommendation:

  • VerifyName checks the characters and verifies that the name is valid. The method returns the name if it's valid, and throws an exception if it isn't.

  • VerifyNCName performs the same validation, but accepts non-qualified names.

The XmlConvert contains additional methods that validate tokens, white-space characters, public IDs, and other strings.

XmlConvert also provides methods that enable you to convert data from a string to a strongly typed data type. For example, the ToDateTime method converts a string to its DateTime equivalent. This is useful because most methods in the XmlReader class return data as a string. After the data is read, it can be converted to the proper data type before being used. The ToString overloads provide the complementary operation by converting strongly typed data to strings. For example, this is useful when you want to add the data to text boxes on a webpage. Locale settings are not taken into account during data conversion. The data types are based on the XML Schema (XSD) data types.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

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

Return to top
Show: