Udostępnij za pośrednictwem


XmlConvert Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Encodes and decodes XML names and provides methods for converting between common language runtime types. When converting data types the values returned are locale independent.

Inheritance Hierarchy

System.Object
  System.Xml.XmlConvert

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

Syntax

'Declaration
Public Class XmlConvert
public class XmlConvert

The XmlConvert type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XmlConvert Initializes a new instance of the XmlConvert class.

Top

Methods

  Name Description
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 DecodeName Decodes a name. This method does the reverse of the EncodeName and EncodeLocalName methods.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 EncodeLocalName Converts the name to a valid XML local name.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 EncodeName Converts the name to a valid XML name.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 EncodeNmToken Verifies the name is valid according to the XML specification.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsNCNameChar Checks if the passed-in character is a valid Name Character type.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsPublicIdChar Checks if the passed in character is a valid PUBLIC ID character.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsStartNCNameChar Checks if the passed-in character is a valid Start Name Character type.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsWhitespaceChar Checks if the passed in character is a valid XML whitespace character.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsXmlChar Checks if the passed in character is a valid XML character.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsXmlSurrogatePair Checks if the passed in surrogate pair of characters is a valid XML character.
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToBoolean Converts the String to a Boolean equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToByte Converts the String to a Byte equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToChar Converts the String to a Char equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToDateTime(String, String) Converts the String to a DateTime equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToDateTime(String, array<String[]) Converts the String to a DateTime equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToDateTime(String, XmlDateTimeSerializationMode) Converts the String to a DateTime using the XmlDateTimeSerializationMode specified
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToDateTimeOffset(String) Converts the supplied String to a DateTimeOffset equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToDateTimeOffset(String, String) Converts the supplied String to a DateTimeOffset equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToDateTimeOffset(String, array<String[]) Converts the supplied String to a DateTimeOffset equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToDecimal Converts the String to a Decimal equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToDouble Converts the String to a Double equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToGuid Converts the String to a Guid equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToInt16 Converts the String to a Int16 equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToInt32 Converts the String to a Int32 equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToInt64 Converts the String to a Int64 equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToSByte Converts the String to a SByte equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToSingle Converts the String to a Single equivalent.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString() Returns a string that represents the current object. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(Boolean) Converts the Boolean to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(Char) Converts the Char to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(DateTimeOffset) Converts the supplied DateTimeOffset to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(Decimal) Converts the Decimal to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(Double) Converts the Double to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(Guid) Converts the Guid to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(Int16) Converts the Int16 to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(Int32) Converts the Int32 to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(Int64) Converts the Int64 to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(SByte) Converts the SByte to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(Single) Converts the Single to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(TimeSpan) Converts the TimeSpan to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(UInt32) Converts the UInt32 to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(UInt64) Converts the UInt64 to a String.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(DateTime, XmlDateTimeSerializationMode) Converts the DateTime to a String using the XmlDateTimeSerializationMode specified.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString(DateTimeOffset, String) Converts the supplied DateTimeOffset to a String in the specified format.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToTimeSpan Converts the String to a TimeSpan equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToUInt16 Converts the String to a UInt16 equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToUInt32 Converts the String to a UInt32 equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToUInt64 Converts the String to a UInt64 equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 VerifyName Verifies that the name is a valid name according to the W3C Extended Markup Language recommendation.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 VerifyNCName Verifies that the name is a valid NCName according to the W3C Extended Markup Language recommendation.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 VerifyNMTOKEN Verifies that the string is a valid NMTOKEN according to the W3C XML Schema Part2: Datatypes recommendation
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 VerifyPublicId Verifies whether all the character is in the string argument are valid PUBLIC ID characters.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 VerifyWhitespace Verifies whether all the characters in the string argument are valid whitespace characters.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 VerifyXmlChars Verifies if all the characters and surrogate pair characters in the passed in string are valid XML characters.

Top

Remarks

Element and attribute names or ID values are limited to a range of XML characters according to the Extensible Markup Language (XML) 1.0 (Fourth Edition) recommendation, located at http://www.w3.org/TR/2006/REC-xml-20060816/. 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.

Examples

'Define the order data.  They will be converted to string 
'before being written out.
Dim custID As Int16 = 32632
Dim orderID As String = "367A54"
Dim orderDate As New DateTime()
orderDate = DateTime.Now
Dim price As Double = 19.95

Dim output As New StringBuilder()

'Create a writer that outputs to the console.
Dim writer As XmlWriter = XmlWriter.Create(output)

'Write an element (this one is the root)
writer.WriteStartElement("order")

'Write the order date.
writer.WriteAttributeString("date", XmlConvert.ToString(orderDate, "yyyy-MM-dd"))

'Write the order time.
writer.WriteAttributeString("time", XmlConvert.ToString(orderDate, "HH:mm:ss"))

'Write the order data.
writer.WriteElementString("orderID", orderID)
writer.WriteElementString("custID", XmlConvert.ToString(custID))
writer.WriteElementString("price", XmlConvert.ToString(price))

'Write the close tag for the root element
writer.WriteEndElement()

'Write the XML and close the writer
writer.Close()


' Display the output to the TextBlock control
OutputTextBlock.Text = output.ToString()

//Define the order data.  They will be converted to string 
//before being written out.
Int16 custID = 32632;
String orderID = "367A54";
DateTime orderDate = new DateTime();
orderDate = DateTime.Now;
Double price = 19.95;

StringBuilder output = new StringBuilder();

//Create a writer that outputs to the console.
using (XmlWriter writer = XmlWriter.Create(output))
{
    //Write an element (this one is the root)
    writer.WriteStartElement("order");

    //Write the order date.
    writer.WriteAttributeString("date", XmlConvert.ToString(orderDate, "yyyy-MM-dd"));

    //Write the order time.
    writer.WriteAttributeString("time", XmlConvert.ToString(orderDate, "HH:mm:ss"));

    //Write the order data.
    writer.WriteElementString("orderID", orderID);
    writer.WriteElementString("custID", XmlConvert.ToString(custID));
    writer.WriteElementString("price", XmlConvert.ToString(price));

    //Write the close tag for the root element
    writer.WriteEndElement();

    //Write the XML and close the writer
}

// Display the output to the TextBlock control
OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

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