共用方式為


XmlConvert.EncodeLocalName(String) 方法

定義

將名稱轉換為有效的 XML 區域名稱。

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

參數

name
String

要編碼的名稱。

傳回

編碼的名稱。

範例

下列範例會編碼和解碼名稱。

#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

備註

這個方法與 方法類似 EncodeName ,不同之處在于它會編碼冒號字元,這可確保名稱可以當做命名空間限定名稱的本機名稱部分使用。

例如,如果您傳遞此方法不正確名稱 a:b,它會傳回a_x003a_b,這是有效的本機名稱。

如果 為 namenullSystem.String.Empty ,則方法會傳回相同的值。

適用於

另請參閱