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.

System.Object
  System.Xml.XmlConvert

Namespace:  System.Xml
Assemblies:   System.Xml.ReaderWriter (in System.Xml.ReaderWriter.dll)
  System.Xml (in System.Xml.dll)

public static class XmlConvert

The XmlConvert type exposes the following members.

  NameDescription
Top

  NameDescription
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsDecodeNameDecodes a name. This method does the reverse of the EncodeName and EncodeLocalName methods.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEncodeLocalNameConverts the name to a valid XML local name.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEncodeNameConverts the name to a valid XML name.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEncodeNmTokenVerifies the name is valid according to the XML specification.
Public methodStatic memberSupported by the XNA FrameworkFromBinHexString Obsolete. Converts a BinHex-encoded string to a byte array.
Public methodStatic memberIsNCNameCharChecks whether the passed-in character is a valid non-colon character type.
Public methodStatic memberIsPublicIdCharReturns the passed-in character instance if the character in the argument is a valid public id character, otherwise null.
Public methodStatic memberIsStartNCNameCharChecks if the passed-in character is a valid Start Name Character type.
Public methodStatic memberIsWhitespaceCharChecks if the passed-in character is a valid XML whitespace character.
Public methodStatic memberIsXmlCharChecks if the passed-in character is a valid XML character.
Public methodStatic memberIsXmlSurrogatePairChecks if the passed-in surrogate pair of characters is a valid XML character.
Public methodStatic memberSupported by the XNA FrameworkToBinHexString Obsolete. Converts a byte array to a BinHex-encoded string.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToBooleanConverts the String to a Boolean equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToByteConverts the String to a Byte equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToCharConverts the String to a Char equivalent.
Public methodStatic memberSupported by the XNA FrameworkToDateTime(String) Obsolete. Converts the String to a DateTime equivalent.
Public methodStatic memberSupported by the XNA FrameworkToDateTime(String, XmlDateTimeSerializationMode)Converts the String to a DateTime using the XmlDateTimeSerializationMode specified
Public methodStatic memberSupported by the XNA FrameworkToDateTime(String, String)Converts the String to a DateTime equivalent.
Public methodStatic memberSupported by the XNA FrameworkToDateTime(String, String[])Converts the String to a DateTime equivalent.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsToDateTimeOffset(String)Converts the supplied String to a DateTimeOffset equivalent.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsToDateTimeOffset(String, String)Converts the supplied String to a DateTimeOffset equivalent.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsToDateTimeOffset(String, String[])Converts the supplied String to a DateTimeOffset equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToDecimalConverts the String to a Decimal equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToDoubleConverts the String to a Double equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToGuidConverts the String to a Guid equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToInt16Converts the String to a Int16 equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToInt32Converts the String to a Int32 equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToInt64Converts the String to a Int64 equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToSByteConverts the String to a SByte equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToSingleConverts the String to a Single equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(Boolean)Converts the Boolean to a String.
Public methodStatic memberSupported by the XNA FrameworkToString(Byte)Converts the Byte to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(Char)Converts the Char to a String.
Public methodStatic memberSupported by the XNA FrameworkToString(DateTime) Obsolete. Converts the DateTime to a String.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(DateTimeOffset)Converts the supplied DateTimeOffset to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(Decimal)Converts the Decimal to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(Double)Converts the Double to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(Guid)Converts the Guid to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(Int16)Converts the Int16 to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(Int32)Converts the Int32 to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(Int64)Converts the Int64 to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(SByte)Converts the SByte to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(Single)Converts the Single to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(TimeSpan)Converts the TimeSpan to a String.
Public methodStatic memberSupported by the XNA FrameworkToString(UInt16)Converts the UInt16 to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(UInt32)Converts the UInt32 to a String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(UInt64)Converts the UInt64 to a String.
Public methodStatic memberSupported by the XNA FrameworkToString(DateTime, XmlDateTimeSerializationMode)Converts the DateTime to a String using the XmlDateTimeSerializationMode specified.
Public methodStatic memberSupported by the XNA FrameworkToString(DateTime, String)Converts the DateTime to a String.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsToString(DateTimeOffset, String)Converts the supplied DateTimeOffset to a String in the specified format.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToTimeSpanConverts the String to a TimeSpan equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToUInt16Converts the String to a UInt16 equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToUInt32Converts the String to a UInt32 equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToUInt64Converts the String to a UInt64 equivalent.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsVerifyNameVerifies that the name is a valid name according to the W3C Extended Markup Language recommendation.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsVerifyNCNameVerifies 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.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsVerifyNMTOKENVerifies that the string is a valid NMTOKEN according to the W3C XML Schema Part2: Datatypes recommendation
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsVerifyPublicIdReturns the passed in string instance if all the characters in the string argument are valid public id characters.
Public methodStatic memberSupported by the XNA FrameworkVerifyTOKENVerifies that the string is a valid token according to the W3C XML Schema Part2: Datatypes recommendation.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsVerifyWhitespaceReturns the passed-in string instance if all the characters in the string argument are valid whitespace characters.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsVerifyXmlCharsReturns 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.
Top

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.

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: 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)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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