关于c#:如何在调用.LoadXml()之前检查字符串输入中的有效xml

关于c#:如何在调用.LoadXml()之前检查字符串输入中的有效xml

How to check for valid xml in string input before calling .LoadXml()

我更愿意在不捕获LoadXml()中的异常的情况下进行此操作,并将此结果作为逻辑的一部分。对于不需要自己手动解析XML的解决方案有什么想法吗?我认为vb对此函数的返回值为false,而不是引发xmlException。XML输入由用户提供。非常感谢!

1
2
3
4
5
6
7
8
9
10
11
12
13
if (!loaded)
{
     this.m_xTableStructure = new XmlDocument();
     try
     {
          this.m_xTableStructure.LoadXml(input);
          loaded = true;
     }
     catch
     {
          loaded = false;
     }
}

(P)Just catch the exception.The small overhead from catching an exception drowns compared to parsing the xml.(p)(P)如果你想发挥作用(for stylistic reasons,not for performance),落实这一点:(p)字母名称


(P)使用XMLValitingreader将防止例外,如果你提供你自己的有效的可能性。(p)


(P)I was unable to get xmlvalidaritinger&;validaritioneventhandler to work.XMLException is still thrown for impully formed xml.我用反射镜检查了这个方法。(p)(P)I indeed need to validate 100s of short xhtml fragments per second.(p)字母名称(P)XMLPARSercontext did not work either.(p)(P)有人跟雷吉克斯在一起吗?(p)


(P)如果卡钦对你来说太多,他们可能想在手之前,利用XML计划,验证XML的有效性,以确保XML是OK,但这将证明比卡钦更重要。(p)


(P)As already been said,I'd rather catch the exception,but using XMLPARSERcontext,you could try to parse"manually"and interact any anomaly;however,unless you're parsing 100 xml fragments per second,why not catching the exception?(p)


推荐阅读