System.Xml.XmlConvert.VerifyWhitespace 方法

方法描述

如果字符串参数中的所有字符都是有效的空白字符,则返回传入的字符串实例。

语法定义(C# System.Xml.XmlConvert.VerifyWhitespace 方法 的用法)

public static string VerifyWhitespace(
	string content
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
content System-String 要验证的 String。
返回值 System.String 如果字符串参数中的所有字符都是有效的空白字符,则返回传入的字符串实例;否则返回 null。

提示和注释

不应返回除参数中传递的以外的其他值。 对于空格有效的字符在不同 XML 版本之间没有什么差别,因为不需要 xml 版本重载。

请参阅 XML 1.0 规范(第四版)生产 [3] S 了解允许字符的详细信息。

如果该参数为 null,则引发 ArgumentNullException。

如果所有字符都不是有效的白空格字符,则会引发 XmlException,且带有遇到第一个无效字符的信息。

System.Xml.XmlConvert.VerifyWhitespace 方法例子

下面的示例使用 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();
            }

异常

异常 异常描述

命名空间

namespace: System.Xml

程序集: System.Xml(在 System.Xml.dll 中)

版本信息

.NET Framework 受以下版本支持:4 .NET Framework Client Profile 受以下版本支持:4 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。