加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

验证C#字符串是否为有效的XML Schema anyURI

发布时间:2020-12-16 01:32:17 所属栏目:百科 来源:网络整理
导读:有没有更好的方法来验证C#字符串是否是有效的xs:anyURI而不是以下? public bool VerifyAnyURI(string anyUriValue){ bool result = false; try { SoapAnyUri anyUri = new SoapAnyUri(anyUriValue); result = true; } catch (Exception) { } return result
有没有更好的方法来验证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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读