XmlConvert Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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.
public ref class XmlConvert abstract sealed
public ref class XmlConvert
public static class XmlConvert
public class XmlConvert
type XmlConvert = class
Public Class XmlConvert
- Inheritance
-
XmlConvert
Remarks
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.
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 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. |
Name validation
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.
Data type conversion
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.
Constructors
XmlConvert() |
Initializes a new instance of the XmlConvert class. |
Methods
DecodeName(String) |
Decodes a name. This method does the reverse of the EncodeName(String) and EncodeLocalName(String) methods. |
EncodeLocalName(String) |
Converts the name to a valid XML local name. |
EncodeName(String) |
Converts the name to a valid XML name. |
EncodeNmToken(String) |
Verifies the name is valid according to the XML specification. |
IsNCNameChar(Char) |
Checks whether the passed-in character is a valid non-colon character type. |
IsPublicIdChar(Char) |
Returns the passed-in character instance if the character in the argument is a valid public id character, otherwise |
IsStartNCNameChar(Char) |
Checks if the passed-in character is a valid Start Name Character type. |
IsWhitespaceChar(Char) |
Checks if the passed-in character is a valid XML whitespace character. |
IsXmlChar(Char) |
Checks if the passed-in character is a valid XML character. |
IsXmlSurrogatePair(Char, Char) |
Checks if the passed-in surrogate pair of characters is a valid XML character. |
ToBoolean(String) | |
ToByte(String) | |
ToChar(String) | |
ToDateTime(String) |
Obsolete.
Obsolete.
|
ToDateTime(String, String) | |
ToDateTime(String, String[]) | |
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, String[]) |
Converts the supplied String to a DateTimeOffset equivalent. |
ToDecimal(String) | |
ToDouble(String) | |
ToGuid(String) | |
ToInt16(String) | |
ToInt32(String) | |
ToInt64(String) | |
ToSByte(String) | |
ToSingle(String) | |
ToString(Boolean) | |
ToString(Byte) | |
ToString(Char) | |
ToString(DateTime) |
Obsolete.
Obsolete.
|
ToString(DateTime, String) | |
ToString(DateTime, XmlDateTimeSerializationMode) |
Converts the DateTime to a String using the XmlDateTimeSerializationMode specified. |
ToString(DateTimeOffset) |
Converts the supplied DateTimeOffset to a String. |
ToString(DateTimeOffset, String) |
Converts the supplied DateTimeOffset to a String in the specified format. |
ToString(Decimal) | |
ToString(Double) | |
ToString(Guid) | |
ToString(Int16) | |
ToString(Int32) | |
ToString(Int64) | |
ToString(SByte) | |
ToString(Single) | |
ToString(TimeSpan) | |
ToString(UInt16) | |
ToString(UInt32) | |
ToString(UInt64) | |
ToTimeSpan(String) | |
ToUInt16(String) | |
ToUInt32(String) | |
ToUInt64(String) | |
VerifyName(String) |
Verifies that the name is a valid name according to the W3C Extended Markup Language recommendation. |
VerifyNCName(String) |
Verifies that the name is a valid |
VerifyNMTOKEN(String) |
Verifies that the string is a valid NMTOKEN according to the W3C XML Schema Part2: Datatypes recommendation. |
VerifyPublicId(String) |
Returns the passed in string instance if all the characters in the string argument are valid public id characters. |
VerifyTOKEN(String) |
Verifies that the string is a valid token according to the W3C XML Schema Part2: Datatypes recommendation. |
VerifyWhitespace(String) |
Returns the passed-in string instance if all the characters in the string argument are valid whitespace characters. |
VerifyXmlChars(String) |
Returns the passed-in string if all the characters and surrogate pair characters in the string argument are valid XML characters, otherwise an |