45

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

5 Answers 5

69

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;
    }
 }
Sign up to request clarification or add additional context in comments.

4 Comments

This happens on page load in a very high load environment, the overhead from the exception is small, but significant enough that it needs to be avoided.
Have you made the measurements to prove this? Also, remember that the try-catch only imposes its (relatively small) performance penalty if the exception is actually thrown.
Where is the XML coming from? If you are expecting valid XML most of the time, it is not worth the overhead of validating on every call. Just handle the exception and move on.
The cost of validating the XML pre-parse is essentially a full pass over the XML, keeping track of state and checking validity of all the text. I can't believe that's less expensive than the occasional XmlException.
11

Using a XmlValidatingReader will prevent the exceptions, if you provide your own ValidationEventHandler.

Comments

8

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.

I indeed need to validate 100s of short XHTML fragments per second.

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.

Anyone succeed with a regex?

4 Comments

>Anyone succeed with a regx? Reminds me of a famous quote: "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."
You're joking about the regex, right? Wrong tool for the job entirely. It's basically the same problem as this: stackoverflow.com/questions/1732348/…
this invalid text goes through as valid: <root><elem>sdf</elem>>>>></root>
@Amit String <root><elem>sdf</elem>>>>></root> (with XML declaration <?xml version="1.0"?> added) is valid XML. Validity can be proven using W3C Validator.
3

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.

Comments

1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.