XmlConvert.EncodeLocalName Method (String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Converts the name to a valid XML local name.

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

public static string EncodeLocalName(
	string name


Type: System.String

The name to be encoded.

Return Value

Type: System.String

The encoded name.

This method is similar to the EncodeName method except that it encodes the colon character, which guarantees that the name can be used as the local name part of a namespace qualified name.

For example, if you passed this method the invalid name a:b, it returns a_x003a_b, which is a valid local name.

If name is null or String.Empty then you get the same value returned.

The following example encodes and decodes names.

using System;
using System.IO;
using System.Xml;

public class Sample

  public static void Main()

     // Encode and decode a name with spaces.
     string name1 = XmlConvert.EncodeName("Order Detail");
     Console.WriteLine("Encoded name: " + name1);
     Console.WriteLine("Decoded name: " + XmlConvert.DecodeName(name1));

     // Encode and decode a local name.
     string name2 = XmlConvert.EncodeLocalName("a:book");
     Console.WriteLine("Encoded local name: " + name2);
     Console.WriteLine("Decoded local name: " + XmlConvert.DecodeName(name2));


Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top