共用方式為


XmlConvert.EncodeName Method

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

Converts the name to a valid XML name.

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

Syntax

'Declaration
Public Shared Function EncodeName ( _
    name As String _
) As String
public static string EncodeName(
    string name
)

Parameters

Return Value

Type: System.String
Returns the name with any invalid characters replaced by an escape string.

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. For example, Order_Details is not encoded, but Order_x0020_ is encoded as Order_x005f_x0020_. No shortforms are allowed. For example, the forms _x20_ and __ are not generated.

This method guarantees the name is valid according to the XML specification. It allows colons in any position, which means the name may 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 EncodeLocalName for the prefix and local name parts and join the result with a colon.

Examples

Dim output As New StringBuilder()
Dim xmlString As New StringBuilder()

Dim writer As XmlWriter = XmlWriter.Create(xmlString)
Dim tag As String = "item name"

Try
    ' Write the root element.
    writer.WriteStartElement("root")

    writer.WriteStartElement(XmlConvert.VerifyName(tag))

Catch e As XmlException
    output.AppendLine(e.Message)
    output.AppendLine("Convert to a valid name...")

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

    writer.WriteStartElement(XmlConvert.EncodeName(tag))
End Try

writer.WriteString("hammer")
writer.WriteEndElement()

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

writer.Close()

StringBuilder output = new StringBuilder();
StringBuilder xmlString = new StringBuilder();

using (XmlWriter writer = XmlWriter.Create(xmlString))
{
    string tag = "item name";

    try
    {
        // Write the root element.
        writer.WriteStartElement("root");

        writer.WriteStartElement(XmlConvert.VerifyName(tag));

    }
    catch (XmlException e)
    {
        output.AppendLine(e.Message);
        output.AppendLine("Convert to a valid name...");

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

        writer.WriteStartElement(XmlConvert.EncodeName(tag));
    }

    writer.WriteString("hammer");
    writer.WriteEndElement();

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

}

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.