Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How check string is valid or not in XML 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 should you use to validate XML data?

Perform validation by using an XSD schema Build and run the application to validate the XML document by using the XSD schema. The application should report that the XML document is valid.

How do I load an XML document and validate it?

To validate the XML in the DOM, you can validate the XML as it is loaded into the DOM by passing a schema-validating XmlReader to the Load method of the XmlDocument class, or validate a previously unvalidated XML document in the DOM using the Validate method of the XmlDocument class.


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

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


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?


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.


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?