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,则方法返回相同的值。

适用于

另请参阅