Mono Class Library: System.Xml.XmlConvert Overview | Members

System.Xml.XmlConvert.EncodeName Method

Converts a name to a valid XML name. [Edit]

public static string EncodeName (string name)

Parameters

name
A string specifying the name to be encoded. [Edit]

Returns

A string containing the XML name. If name is null or string.Empty, name is returned. [Edit]

Remarks

This method translates invalid characters, such as spaces or half-width Katakana, that need to be mapped to XML names without the support or presence of schemas. The invalid characters are translated into escaped numeric entity encodings.

The escape character is '_'. Any XML name character that does not conform to the W3C Extensible Markup Language (XML) 1.0 specification is escaped as _xHHHH_. The HHHH string stands for the four-digit hexadecimal UCS-2 code for the character in most significant bit first order. For example, the name "Order Details" is encoded as "Order_x0020_Details".

The underscore character does not need to be escaped unless it is followed by a character sequence that together with the underscore can be misinterpreted as an escape sequence when decoding the name. No short forms are encoded. For example, the forms "_x20_" and "__" are not encoded.

This method guarantees the name is valid according to the XML specification. It allows colons in any position, which means the name might still be invalid according to the W3C Namespace Specification (www.w3.org/TR/REC-xml-names). To guarantee it is a valid namespace qualified name use the XmlConvert.EncodeLocalName(string) method for the prefix and local name parts and join the result with a colon.

[Edit]

Example

See the XmlConvert.EncodeLocalName(string) method for an example comparing the XmlConvert.EncodeLocalName(string), XmlConvert.EncodeName(string), and XmlConvert.EncodeNmToken(string) methods.

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0