Redigera

Dela via


XmlConvert.EncodeLocalName(String) Method

Definition

Converts the name to a valid XML local name.

public:
 static System::String ^ EncodeLocalName(System::String ^ name);
public static string EncodeLocalName (string name);
public static string? EncodeLocalName (string? name);
static member EncodeLocalName : string -> string
Public Shared Function EncodeLocalName (name As String) As String

Parameters

name
String

The name to be encoded.

Returns

The encoded name.

Examples

The following example encodes and decodes names.

#using <System.dll>
#using <System.XML.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Encode and decode a name with spaces.
   String^ name1 = XmlConvert::EncodeName( "Order Detail" );
   Console::WriteLine( "Encoded name: {0}", name1 );
   Console::WriteLine( "Decoded name: {0}", XmlConvert::DecodeName( name1 ) );
   
   // Encode and decode a local name.
   String^ name2 = XmlConvert::EncodeLocalName( "a:book" );
   Console::WriteLine( "Encoded local name: {0}", name2 );
   Console::WriteLine( "Decoded local name: {0}", XmlConvert::DecodeName( name2 ) );
}
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));
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

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

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

  end sub
end class

Remarks

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 System.String.Empty, the method returns the same value.

Applies to

See also