XmlConvert XmlConvert XmlConvert XmlConvert Class

定義

編碼和解碼 XML 名稱,並且提供在 Common Language Runtime 類型和 XML 結構描述定義語言 (XSD) 類型之間轉換的方法。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
public class XmlConvert
type XmlConvert = class
Public Class XmlConvert
繼承
XmlConvertXmlConvertXmlConvertXmlConvert

備註

XmlConvert類別在功能上等於Convert類別,但它支援 XML 標準。The XmlConvert class is functionally equivalent to the Convert class, but it supports XML standards. 這種型別系統的架構是 XML 結構描述定義語言 (XSD) 的結構描述型別,因此傳回的值一律不受地區設定影響。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

項目和屬性的名稱或 ID 值受限於範圍的 XML 字元,根據 W3C XML 1.0 版建議事項Element and attribute names or ID values are limited to a range of XML characters according to the W3C XML 1.0 recommendation. 當名稱包含無效的字元時,您可以使用EncodeNameDecodeName這個類別,以將它們轉譯成有效的 XML 名稱中的方法。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. 不過,在 XML 中,[順序] 和 [詳細資料] 之間的空間視為無效的 XML 字元。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.

您可以使用EncodeName方法使用XmlWriter類別,以確保寫入的名稱是有效的 XML 名稱。You can use the EncodeName method with the XmlWriter class to ensure the names being written are valid XML names. 下列 C# 程式碼將 [訂單詳細資料] 的名稱轉換成有效的 XML 名稱,並寫入項目<Order_0x0020_Detail>My order</Order_0x0020_Detail>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方法執行編碼和解碼。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. 這個方法允許冒號出現在任何位置,也就是說,名稱可能仍會根據 W3C 無效Namespaces in XML 1.0 建議事項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 與相同EncodeName不同之處在於它也會編碼冒號字元,保證可用來當做名稱LocalName的命名空間限定名稱的一部分。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

XmlConvert類別包含兩個方法,檢查中的項目或屬性名稱的字元,並確認名稱是否符合 W3C 所設定的規則XML 1.0 版建議事項: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 檢查字元,並確認名稱有效。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 執行相同的驗證,但可接受非限定名稱。VerifyNCName performs the same validation, but accepts non-qualified names.

XmlConvert包含驗證權杖、 泛空白字元、 公用識別碼和其他字串的其他方法。The XmlConvert contains additional methods that validate tokens, white-space characters, public IDs, and other strings.

資料類型轉換Data type conversion

XmlConvert 也提供可讓您將資料從字串轉換成強類型的資料類型方法。XmlConvert also provides methods that enable you to convert data from a string to a strongly typed data type. 例如,ToDateTime方法將字串轉換成其DateTime相等。For example, the ToDateTime method converts a string to its DateTime equivalent. 使用此方法因為中的大部分方法XmlReader類別傳回的資料,做為字串。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. ToString多載會提供互補的作業,藉由將強型別的資料轉換為字串。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. 資料類型會根據 XML 結構描述 (XSD) 資料型別。The data types are based on the XML Schema (XSD) data types.

建構函式

XmlConvert() XmlConvert() XmlConvert() XmlConvert()

初始化 XmlConvert 類別的新執行個體。Initializes a new instance of the XmlConvert class.

方法

DecodeName(String) DecodeName(String) DecodeName(String) DecodeName(String)

將名稱解碼。Decodes a name. 這個方法反向執行 EncodeName(String)EncodeLocalName(String) 方法。This method does the reverse of the EncodeName(String) and EncodeLocalName(String) methods.

EncodeLocalName(String) EncodeLocalName(String) EncodeLocalName(String) EncodeLocalName(String)

將名稱轉換為有效的 XML 區域名稱。Converts the name to a valid XML local name.

EncodeName(String) EncodeName(String) EncodeName(String) EncodeName(String)

將名稱轉換為有效的 XML 名稱。Converts the name to a valid XML name.

EncodeNmToken(String) EncodeNmToken(String) EncodeNmToken(String) EncodeNmToken(String)

根據 XML 規格驗證確定名稱有效。Verifies the name is valid according to the XML specification.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsNCNameChar(Char) IsNCNameChar(Char) IsNCNameChar(Char) IsNCNameChar(Char)

檢查傳入的字元是否為有效的非冒號字元型別。Checks whether the passed-in character is a valid non-colon character type.

IsPublicIdChar(Char) IsPublicIdChar(Char) IsPublicIdChar(Char) IsPublicIdChar(Char)

如果引數中的字元是有效的公用 ID 字元,則會傳回傳入的字元執行個體,否則傳回 nullReturns the passed-in character instance if the character in the argument is a valid public id character, otherwise null.

IsStartNCNameChar(Char) IsStartNCNameChar(Char) IsStartNCNameChar(Char) IsStartNCNameChar(Char)

檢查傳入的字元是否為有效的開頭名稱字元型別。Checks if the passed-in character is a valid Start Name Character type.

IsWhitespaceChar(Char) IsWhitespaceChar(Char) IsWhitespaceChar(Char) IsWhitespaceChar(Char)

檢查傳入的字元是否為有效的 XML 空白字元。Checks if the passed-in character is a valid XML whitespace character.

IsXmlChar(Char) IsXmlChar(Char) IsXmlChar(Char) IsXmlChar(Char)

檢查傳入的字元是否為有效的 XML 字元。Checks if the passed-in character is a valid XML character.

IsXmlSurrogatePair(Char, Char) IsXmlSurrogatePair(Char, Char) IsXmlSurrogatePair(Char, Char) IsXmlSurrogatePair(Char, Char)

檢查傳入的字元 Surrogate 字組是否為有效的 XML 字元。Checks if the passed-in surrogate pair of characters is a valid XML character.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToBoolean(String) ToBoolean(String) ToBoolean(String) ToBoolean(String)

String 轉換成對等的 BooleanConverts the String to a Boolean equivalent.

ToByte(String) ToByte(String) ToByte(String) ToByte(String)

String 轉換成對等的 ByteConverts the String to a Byte equivalent.

ToChar(String) ToChar(String) ToChar(String) ToChar(String)

String 轉換成對等的 CharConverts the String to a Char equivalent.

ToDateTime(String) ToDateTime(String) ToDateTime(String) ToDateTime(String)

String 轉換成對等的 DateTimeConverts the String to a DateTime equivalent.

ToDateTime(String, String) ToDateTime(String, String) ToDateTime(String, String) ToDateTime(String, String)

String 轉換成對等的 DateTimeConverts the String to a DateTime equivalent.

ToDateTime(String, String[]) ToDateTime(String, String[]) ToDateTime(String, String[]) ToDateTime(String, String[])

String 轉換成對等的 DateTimeConverts the String to a DateTime equivalent.

ToDateTime(String, XmlDateTimeSerializationMode) ToDateTime(String, XmlDateTimeSerializationMode) ToDateTime(String, XmlDateTimeSerializationMode) ToDateTime(String, XmlDateTimeSerializationMode)

使用指定的 XmlDateTimeSerializationMode,將 String 轉換為 DateTimeConverts the String to a DateTime using the XmlDateTimeSerializationMode specified

ToDateTimeOffset(String) ToDateTimeOffset(String) ToDateTimeOffset(String) ToDateTimeOffset(String)

將提供的 String 轉換成 DateTimeOffset 對等用法。Converts the supplied String to a DateTimeOffset equivalent.

ToDateTimeOffset(String, String) ToDateTimeOffset(String, String) ToDateTimeOffset(String, String) ToDateTimeOffset(String, String)

將提供的 String 轉換成 DateTimeOffset 對等用法。Converts the supplied String to a DateTimeOffset equivalent.

ToDateTimeOffset(String, String[]) ToDateTimeOffset(String, String[]) ToDateTimeOffset(String, String[]) ToDateTimeOffset(String, String[])

將提供的 String 轉換成 DateTimeOffset 對等用法。Converts the supplied String to a DateTimeOffset equivalent.

ToDecimal(String) ToDecimal(String) ToDecimal(String) ToDecimal(String)

String 轉換成對等的 DecimalConverts the String to a Decimal equivalent.

ToDouble(String) ToDouble(String) ToDouble(String) ToDouble(String)

String 轉換成對等的 DoubleConverts the String to a Double equivalent.

ToGuid(String) ToGuid(String) ToGuid(String) ToGuid(String)

String 轉換成對等的 GuidConverts the String to a Guid equivalent.

ToInt16(String) ToInt16(String) ToInt16(String) ToInt16(String)

String 轉換成對等的 Int16Converts the String to a Int16 equivalent.

ToInt32(String) ToInt32(String) ToInt32(String) ToInt32(String)

String 轉換成對等的 Int32Converts the String to a Int32 equivalent.

ToInt64(String) ToInt64(String) ToInt64(String) ToInt64(String)

String 轉換成對等的 Int64Converts the String to a Int64 equivalent.

ToSByte(String) ToSByte(String) ToSByte(String) ToSByte(String)

String 轉換成對等的 SByteConverts the String to a SByte equivalent.

ToSingle(String) ToSingle(String) ToSingle(String) ToSingle(String)

String 轉換成對等的 SingleConverts the String to a Single equivalent.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
ToString(Boolean) ToString(Boolean) ToString(Boolean) ToString(Boolean)

Boolean 轉換成 StringConverts the Boolean to a String.

ToString(Byte) ToString(Byte) ToString(Byte) ToString(Byte)

Byte 轉換成 StringConverts the Byte to a String.

ToString(Char) ToString(Char) ToString(Char) ToString(Char)

Char 轉換成 StringConverts the Char to a String.

ToString(DateTime) ToString(DateTime) ToString(DateTime) ToString(DateTime)

DateTime 轉換成 StringConverts the DateTime to a String.

ToString(DateTime, String) ToString(DateTime, String) ToString(DateTime, String) ToString(DateTime, String)

DateTime 轉換成 StringConverts the DateTime to a String.

ToString(DateTime, XmlDateTimeSerializationMode) ToString(DateTime, XmlDateTimeSerializationMode) ToString(DateTime, XmlDateTimeSerializationMode) ToString(DateTime, XmlDateTimeSerializationMode)

使用指定的 XmlDateTimeSerializationMode,將 DateTime 轉換為 StringConverts the DateTime to a String using the XmlDateTimeSerializationMode specified.

ToString(DateTimeOffset) ToString(DateTimeOffset) ToString(DateTimeOffset) ToString(DateTimeOffset)

將提供的 DateTimeOffset 轉換成 StringConverts the supplied DateTimeOffset to a String.

ToString(DateTimeOffset, String) ToString(DateTimeOffset, String) ToString(DateTimeOffset, String) ToString(DateTimeOffset, String)

將提供的 DateTimeOffset 轉換成指定格式的 StringConverts the supplied DateTimeOffset to a String in the specified format.

ToString(Decimal) ToString(Decimal) ToString(Decimal) ToString(Decimal)

Decimal 轉換成 StringConverts the Decimal to a String.

ToString(Double) ToString(Double) ToString(Double) ToString(Double)

Double 轉換成 StringConverts the Double to a String.

ToString(Guid) ToString(Guid) ToString(Guid) ToString(Guid)

Guid 轉換成 StringConverts the Guid to a String.

ToString(Int16) ToString(Int16) ToString(Int16) ToString(Int16)

Int16 轉換成 StringConverts the Int16 to a String.

ToString(Int32) ToString(Int32) ToString(Int32) ToString(Int32)

Int32 轉換成 StringConverts the Int32 to a String.

ToString(Int64) ToString(Int64) ToString(Int64) ToString(Int64)

Int64 轉換成 StringConverts the Int64 to a String.

ToString(SByte) ToString(SByte) ToString(SByte) ToString(SByte)

SByte 轉換成 StringConverts the SByte to a String.

ToString(Single) ToString(Single) ToString(Single) ToString(Single)

Single 轉換成 StringConverts the Single to a String.

ToString(TimeSpan) ToString(TimeSpan) ToString(TimeSpan) ToString(TimeSpan)

TimeSpan 轉換成 StringConverts the TimeSpan to a String.

ToString(UInt16) ToString(UInt16) ToString(UInt16) ToString(UInt16)

UInt16 轉換成 StringConverts the UInt16 to a String.

ToString(UInt32) ToString(UInt32) ToString(UInt32) ToString(UInt32)

UInt32 轉換成 StringConverts the UInt32 to a String.

ToString(UInt64) ToString(UInt64) ToString(UInt64) ToString(UInt64)

UInt64 轉換成 StringConverts the UInt64 to a String.

ToTimeSpan(String) ToTimeSpan(String) ToTimeSpan(String) ToTimeSpan(String)

String 轉換成對等的 TimeSpanConverts the String to a TimeSpan equivalent.

ToUInt16(String) ToUInt16(String) ToUInt16(String) ToUInt16(String)

String 轉換成對等的 UInt16Converts the String to a UInt16 equivalent.

ToUInt32(String) ToUInt32(String) ToUInt32(String) ToUInt32(String)

String 轉換成對等的 UInt32Converts the String to a UInt32 equivalent.

ToUInt64(String) ToUInt64(String) ToUInt64(String) ToUInt64(String)

String 轉換成對等的 UInt64Converts the String to a UInt64 equivalent.

VerifyName(String) VerifyName(String) VerifyName(String) VerifyName(String)

根據 W3C Extended Markup Language Recommendation,驗證確定名稱是有效的名稱。Verifies that the name is a valid name according to the W3C Extended Markup Language recommendation.

VerifyNCName(String) VerifyNCName(String) VerifyNCName(String) VerifyNCName(String)

根據 W3C Extended Markup Language Recommendation,驗證確定名稱是有效的 NCNameVerifies that the name is a valid NCName according to the W3C Extended Markup Language recommendation. NCName 是不能包含冒號的名稱。An NCName is a name that cannot contain a colon.

VerifyNMTOKEN(String) VerifyNMTOKEN(String) VerifyNMTOKEN(String) VerifyNMTOKEN(String)

根據<W3C XML 結構描述第 2 部分:資料型別>(英文) 建議,驗證字串是否為有效的 NMTOKEN。Verifies that the string is a valid NMTOKEN according to the W3C XML Schema Part2: Datatypes recommendation

VerifyPublicId(String) VerifyPublicId(String) VerifyPublicId(String) VerifyPublicId(String)

如果字串引數中的所有字元都是有效的公用 ID 字元,則會傳回傳入的字串執行個體。Returns the passed in string instance if all the characters in the string argument are valid public id characters.

VerifyTOKEN(String) VerifyTOKEN(String) VerifyTOKEN(String) VerifyTOKEN(String)

根據<W3C XML 結構描述第 2 部分:資料型別建議事項>(英文) 驗證字串是否為有效的權杖。Verifies that the string is a valid token according to the W3C XML Schema Part2: Datatypes recommendation.

VerifyWhitespace(String) VerifyWhitespace(String) VerifyWhitespace(String) VerifyWhitespace(String)

如果字串引數中的所有字元都是有效的空白字元,則會傳回傳入的字串執行個體。Returns the passed-in string instance if all the characters in the string argument are valid whitespace characters.

VerifyXmlChars(String) VerifyXmlChars(String) VerifyXmlChars(String) VerifyXmlChars(String)

如果字串引數中的所有字元及 Surrogate 字組字元都是有效的 XML 字元,則傳回傳入的字串,否則擲回 XmlException,並提供遇到的第一個無效字元的相關資訊。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.

適用於