Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check well-formed XML without a try/catch?

Does anyone know how I can check if a string contains well-formed XML without using something like XmlDocument.LoadXml() in a try/catch block? I've got input that may or may not be XML, and I want code that recognises that input may not be XML without relying on a try/catch, for both speed and on the general principle that non-exceptional circumstances shouldn't raise exceptions. I currently have code that does this;

private bool IsValidXML(string value)     {         try         {             // Check we actually have a value             if (string.IsNullOrEmpty(value) == false)             {                 // Try to load the value into a document                 XmlDocument xmlDoc = new XmlDocument();                  xmlDoc.LoadXml(value);                  // If we managed with no exception then this is valid XML!                 return true;             }             else             {                 // A blank value is not valid xml                 return false;             }         }         catch (System.Xml.XmlException)         {             return false;         }     } 

But it seems like something that shouldn't require the try/catch. The exception is causing merry hell during debugging because every time I check a string the debugger will break here, 'helping' me with my pesky problem.

like image 743
Steve Cooper Avatar asked Jun 22 '09 09:06

Steve Cooper


People also ask

How to check XML is valid or not in c#?

To validate the content of XML file, first create an XML text reader to work file and use reader to initialize instance of validating reader. It can be initialized by living instance of xmlReader classs. The below content is looped to validate the XML document, public bool ValidateDocument ( string fileName )

What is XML well Formedness?

Well-Formed XML DocumentsAn XML document that adheres to proper XML syntax is said to be well-formed. If the results of parsing are to be presented by an XML processor (also known as an XML parser) to its associated application, the XML document must be well-formed.


2 Answers

I don't know a way of validating without the exception, but you can change the debugger settings to only break for XmlException if it's unhandled - that should solve your immediate issues, even if the code is still inelegant.

To do this, go to Debug / Exceptions... / Common Language Runtime Exceptions and find System.Xml.XmlException, then make sure only "User-unhandled" is ticked (not Thrown).

like image 106
Jon Skeet Avatar answered Oct 01 '22 23:10

Jon Skeet


Steve,

We had an 3rd party that accidentally sometimes sent us JSON instead of XML. Here is what I implemented:

public static bool IsValidXml(string xmlString) {     Regex tagsWithData = new Regex("<\\w+>[^<]+</\\w+>");      //Light checking     if (string.IsNullOrEmpty(xmlString) || tagsWithData.IsMatch(xmlString) == false)     {         return false;     }      try     {         XmlDocument xmlDocument = new XmlDocument();         xmlDocument.LoadXml(xmlString);         return true;     }     catch (Exception e1)     {         return false;     } }  [TestMethod()] public void TestValidXml() {     string xml = "<result>true</result>";     Assert.IsTrue(Utility.IsValidXml(xml)); }  [TestMethod()] public void TestIsNotValidXml() {     string json = "{ \"result\": \"true\" }";     Assert.IsFalse(Utility.IsValidXml(json)); } 
like image 35
Greg Finzer Avatar answered Oct 02 '22 00:10

Greg Finzer