C# 如何在调用 .LoadXml() 之前检查字符串输入中的有效 xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/86292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check for valid xml in string input before calling .LoadXml()
提问by Chris Ballance
I would much prefer to do this without catching an exception in LoadXml()
and using this results as part of my logic. Any ideas for a solution that doesn't involve manually parsing the xml myself? I think VB has a return value of false for this function instead of throwing an XmlException. Xml input is provided from the user. Thanks much!
我更愿意在不捕获异常的情况下执行此操作,LoadXml()
并将此结果用作我的逻辑的一部分。关于不涉及自己手动解析 xml 的解决方案的任何想法?我认为 VB 对此函数的返回值为 false,而不是抛出 XmlException。Xml 输入由用户提供。非常感谢!
if (!loaded)
{
this.m_xTableStructure = new XmlDocument();
try
{
this.m_xTableStructure.LoadXml(input);
loaded = true;
}
catch
{
loaded = false;
}
}
采纳答案by Rasmus Faber
Just catch the exception. The small overhead from catching an exception drowns compared to parsing the XML.
只需捕获异常。与解析 XML 相比,捕获异常的小开销淹没了。
If you want the function (for stylistic reasons, not for performance), implement it yourself:
如果您想要该功能(出于风格原因,而不是为了性能),请自行实现:
public class MyXmlDocument: XmlDocument
{
bool TryParseXml(string xml){
try{
ParseXml(xml);
return true;
}catch(XmlException e){
return false;
}
}
回答by Sunny Milenov
Using a XmlValidatingReader will prevent the exceptions, if you provide your own ValidationEventHandler.
如果您提供自己的 ValidationEventHandler,则使用 XmlValidatingReader 将防止出现异常。
回答by Martin Marconcini
AS already been said, I'd rather catch the exception, but using XmlParserContext, you could try to parse "manually" and intercept any anomaly; however, unless you're parsing 100 xml fragments per second, why not catching the exception?
正如已经说过的那样,我宁愿捕获异常,但是使用XmlParserContext,您可以尝试“手动”解析并拦截任何异常;但是,除非您每秒解析 100 个 xml 片段,否则为什么不捕获异常呢?
回答by Martin Marconcini
If catching is too much for you, then you might want to validate the XML beforehand, using an XML Schema, to make sure that the XML is ok, But that will probably be worse than catching.
如果捕获对您来说太过分了,那么您可能需要使用 XML 模式预先验证 XML,以确保 XML 正常,但这可能比捕获更糟糕。
回答by Dan
I was unable to get XmlValidatingReader & ValidationEventHandler to work. The XmlException is still thrown for incorrectly formed xml. I verified this by viewing the methods with reflector.
我无法让 XmlValidatingReader 和 ValidationEventHandler 工作。对于格式不正确的 xml,仍会抛出 XmlException。我通过使用反射器查看方法来验证这一点。
I indeed need to validate 100s of short XHTML fragments per second.
我确实需要每秒验证 100 个短 XHTML 片段。
public static bool IsValidXhtml(this string text)
{
bool errored = false;
var reader = new XmlValidatingReader(text, XmlNodeType.Element, new XmlParserContext(null, new XmlNamespaceManager(new NameTable()), null, XmlSpace.None));
reader.ValidationEventHandler += ((sender, e) => { errored = e.Severity == System.Xml.Schema.XmlSeverityType.Error; });
while (reader.Read()) { ; }
reader.Close();
return !errored;
}
XmlParserContext did not work either.
XmlParserContext 也不起作用。
Anyone succeed with a regex?
有人用正则表达式成功吗?