如何调用.LoadXml之前检查的字符串输入有效的XML() [英] How to check for valid xml in string input before calling .LoadXml()

查看:255
本文介绍了如何调用.LoadXml之前检查的字符串输入有效的XML()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更preFER要做到这一点,而不在 loadXML的捕获异常()并使用此结果我的逻辑的一部分。任何想法,这并不涉及手工解析XML自己的解决方案?我认为VB有假此功能,而不是抛出XmlException返回值。 xml的输入是从用户提供的。谢谢了!

 如果(!加载)
{
     this.m_xTableStructure =新的XmlDocument();
     尝试
     {
          this.m_xTableStructure.LoadXml(输入);
          加载=真正的;
     }
     抓住
     {
          加载= FALSE;
     }
}
 

解决方案

刚刚捕获异常。从捕获异常的小的开销淹没相比,解析XML。

如果你想要的功能(文体的原因,而不是性能),实现它自己的:

 公共类MyXmlDocument:XmlDocument的
{
  布尔TryParseXml(XML字符串){
    尝试{
      ParseXml(XML);
      返回true;
    }赶上(XmlException E){
      返回false;
    }
 }
 

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!

if (!loaded)
{
     this.m_xTableStructure = new XmlDocument();
     try
     {
          this.m_xTableStructure.LoadXml(input);
          loaded = true;
     }
     catch
     {
          loaded = false;
     }
}

解决方案

Just catch the exception. The small overhead from catching an exception drowns compared to parsing the 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;
    }
 }

这篇关于如何调用.LoadXml之前检查的字符串输入有效的XML()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆