c# - 将 C# 字符串验证为有效的 XML 模式 anyURI

标签 c# parsing xsd

是否有比以下方法更好的验证 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/

相关文章:

c# - Roslyn 在指定节点后插入节点

parsing - 现代编程语言的语法是上下文无关的还是上下文敏感的?

java 。 XML。如何根据 XSD 架构验证 xml 文档的某些部分?

c# - 使用 XSD.exe 生成 C# 帮助程序类 : Failed handling imported schemas

c# - 参数匹配不能与 NSubstitute 一起正常工作

c# - 如何正确地将对象按类型从 C# 传递到 VBScript?

c# - 无法加载文件或程序集 'accord.video.ffmpeg.x64.dll' 或其依赖项之一

c++ - c/c++编译器如何知道错误在哪一行

ios - 需要帮助将这些 JSON 数据获取(解析?)到 Xcode 中

php - PHP libxml 中针对 XSD 的 XML 验证