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)
Thetype exposes the following members.
|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 the default hash function. (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 an XmlException is thrown with information on the first invalid character encountered.|
The 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.
Encoding and decoding
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 methods perform encoding and decoding.
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.
Takes a name and returns the encoded name.
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.
Reverses the transformation for all the encoding methods.
The 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 contains additional methods that validate tokens, white-space characters, public IDs, and other strings.
Data type conversion
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.
.NET FrameworkSupported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)