XmlConvert.VerifyName(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
根据 W3C 可扩展标记语言建议验证该名称是否是有效的名称。
public:
static System::String ^ VerifyName(System::String ^ name);
public static string VerifyName (string name);
static member VerifyName : string -> string
Public Shared Function VerifyName (name As String) As String
参数
- name
- String
要验证的名称。
返回
该名称(如果它是有效的 XML 名称)。
例外
name
不是有效的 XML 名称。
name
为 null
或 String.Empty。
示例
以下示例使用 VerifyName
该方法编写元素名称。
#using <System.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
int main()
{
XmlTextWriter^ writer = gcnew XmlTextWriter( "out.xml", nullptr );
String^ tag = "item name";
try
{
// Write the root element.
writer->WriteStartElement( "root" );
writer->WriteStartElement( XmlConvert::VerifyName( tag ) );
}
catch ( XmlException^ e )
{
Console::WriteLine( e->Message );
Console::WriteLine( "Convert to a valid name..." );
writer->WriteStartElement( XmlConvert::EncodeName( tag ) );
}
writer->WriteString( "hammer" );
writer->WriteEndElement();
// Write the end tag for the root element.
writer->WriteEndElement();
writer->Close();
}
using System;
using System.Xml;
public class Sample{
public static void Main(){
XmlTextWriter writer = new XmlTextWriter ("out.xml", null);
string tag = "item name";
try{
// Write the root element.
writer.WriteStartElement("root");
writer.WriteStartElement(XmlConvert.VerifyName(tag));
}
catch (XmlException e){
Console.WriteLine(e.Message);
Console.WriteLine("Convert to a valid name...");
writer.WriteStartElement(XmlConvert.EncodeName(tag));
}
writer.WriteString("hammer");
writer.WriteEndElement();
// Write the end tag for the root element.
writer.WriteEndElement();
writer.Close();
}
}
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim writer As New XmlTextWriter("out.xml", Nothing)
Dim tag As String = "item name"
Try
' Write the root element.
writer.WriteStartElement("root")
writer.WriteStartElement(XmlConvert.VerifyName(tag))
Catch e As XmlException
Console.WriteLine(e.Message)
Console.WriteLine("Convert to a valid name...")
writer.WriteStartElement(XmlConvert.EncodeName(tag))
End Try
writer.WriteString("hammer")
writer.WriteEndElement()
' Write the end tag for the root element.
writer.WriteEndElement()
writer.Close()
End Sub
End Class
注解
此方法可以采用以下方式与类一起使用 XmlWriter 。
try{
writer.WriteStartElement(XmlConvert.VerifyName("item"),"bar");
}
catch(Exception e)
{
Console.WriteLine("error");
}