XmlConvert.VerifyWhitespace(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
若字串參數中的所有字元皆為有效的空白字元,則會回傳傳入的字串實例。
public:
static System::String ^ VerifyWhitespace(System::String ^ content);
public static string VerifyWhitespace(string content);
static member VerifyWhitespace : string -> string
Public Shared Function VerifyWhitespace (content As String) As String
參數
傳回
若字串參數中所有字元皆為有效空白字元,則為傳入字串實例,否則 null。
範例
以下範例使用該 VerifyWhitespace 方法來偵測起始元素值中是否有無效字元。
XmlTextWriter writer5 = new XmlTextWriter("outFile.xml", null);
char illegalWhiteSpaceChar = '_';
try
{
// Write the root element.
writer5.WriteStartElement("root");
writer5.WriteStartElement("legalElement");
// Throw an exception due illegal white space character.
writer5.WriteString("ValueText" +
XmlConvert.VerifyWhitespace("\t" + illegalWhiteSpaceChar));
// Write the end tag for the legal element.
writer5.WriteEndElement();
// Write the end tag for the root element.
writer5.WriteEndElement();
writer5.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
writer5.Close();
}
Dim writer5 As XmlTextWriter = New XmlTextWriter("outFile.xml", Nothing)
Dim illegalWhiteSpaceChar As Char = "_"
Try
' Write the root element.
writer5.WriteStartElement("root")
writer5.WriteStartElement("legalElement")
' Throw an exception due illegal white space character.
writer5.WriteString("ValueText" + _
XmlConvert.VerifyWhitespace(" " + illegalWhiteSpaceChar))
writer5.WriteEndElement()
' Write the end tag for the root element.
writer5.WriteEndElement()
writer5.Close()
Catch e As XmlException
Console.WriteLine(e.Message)
writer5.Close()
End Try
備註
除了傳入參數外,不應回傳其他值。 可用於空白的字元在不同 XML 版本間不會有差異,因此不需要 XML 版本過載。
關於允許字元的詳細資訊,請參見 XML 1.0 規範(第四版) 生產 [3] S。
若參數為空,則會拋出 。ArgumentNullException
如果任何字元不是有效的空白字元,則會拋出 an XmlException 並顯示第一個遇到的無效字元資訊。