是否有比以下方法更好的验证 C# 字符串是否为有效 xs:anyURI 的方法?
public bool VerifyAnyURI(string anyUriValue)
{
bool result = false;
try
{
SoapAnyUri anyUri = new SoapAnyUri(anyUriValue);
result = true;
}
catch (Exception)
{
}
return result;
}
这个构造函数似乎没有抛出任何类型的异常。任何有效的字符串在技术上都是有效的 anyURI 吗?
最佳答案
查看带有 Reflector 的 SoapAnyUri
构造函数,它根本不执行任何验证。
anyURI
数据类型 is defined as follows :
[Definition:] anyURI represents a Uniform Resource Identifier Reference (URI). An anyURI value can be absolute or relative, and may have an optional fragment identifier (i.e., it may be a URI Reference). This type should be used to specify the intention that the value fulfills the role of a URI as defined by [RFC 2396], as amended by [RFC 2732].
所以你可以简单地使用 Uri.TryCreate
使用 UriKind.RelativeOrAbsolute
验证字符串是否代表有效值:
Uri uri;
bool valid = Uri.TryCreate(anyUriValue, UriKind.RelativeOrAbsolute, out uri);
关于c# - 将 C# 字符串验证为有效的 XML 模式 anyURI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1433893/