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

后端 未结 5 510
一个人的身影
一个人的身影 2020-11-30 05:48

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 in

5条回答
  •  借酒劲吻你
    2020-11-30 06:16

    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;
        }
     }
    

提交回复
热议问题