How to handle fixing XML validation errors in C#.NET?
-
Can somebody tell me how to handle fixing XML validation errors? In my code, I call xmlDoc.Validate(Schemas_ValidationEventHandler); in my main method to validate the Xml document that I have just created. This calls the below eventhandler for each validation error. Based on the error type, i want to be able to perform various actions on the problematic node (example: delete the node) static void Schemas_ValidationEventHandler(object sender, ValidationEventArgs e) { System.Xml.Schema.XmlSchemaValidationException ex = (XmlSchemaValidationException)e.Exception; //I want to delete the node 'ex.SourceObject' if the content is empty. //How do I get the document node?? }
-
Can somebody tell me how to handle fixing XML validation errors? In my code, I call xmlDoc.Validate(Schemas_ValidationEventHandler); in my main method to validate the Xml document that I have just created. This calls the below eventhandler for each validation error. Based on the error type, i want to be able to perform various actions on the problematic node (example: delete the node) static void Schemas_ValidationEventHandler(object sender, ValidationEventArgs e) { System.Xml.Schema.XmlSchemaValidationException ex = (XmlSchemaValidationException)e.Exception; //I want to delete the node 'ex.SourceObject' if the content is empty. //How do I get the document node?? }
They hide that information in the documentation XmlSchemaValidationException.SourceObject[^]
When an XmlSchemaValidationException is thrown during validation of a class that implements the IXPathNavigable interface such as the XPathNavigator or XmlNode class, the object returned by the SourceObject property is an instance of a class that implements the IXPathNavigable interface.
When an
-
They hide that information in the documentation XmlSchemaValidationException.SourceObject[^]
When an XmlSchemaValidationException is thrown during validation of a class that implements the IXPathNavigable interface such as the XPathNavigator or XmlNode class, the object returned by the SourceObject property is an instance of a class that implements the IXPathNavigable interface.
When an
Below is what I did to be able to delete problematic elements(does not conform to schema) from an XML file. 1. In the event handler, I set the innertext of error nodes to 'ERROR' static void Schemas_ValidationEventHandler(object sender, ValidationEventArgs e) { //Get the Schema Validation Exception System.Xml.Schema.XmlSchemaValidationException ex = (XmlSchemaValidationException)e.Exception; //Get the error Xml Node XmlNode node = (XmlNode)ex.SourceObject; //Set the text to ERROR so that it can be removed later //NOTE: trying to delete error nodes here as part of validation causes the validation to break //that is why i used this method of marking problematic nodes and removing them later node.InnerText = "ERROR"; } 2. Then I have a method to remove 'ERROR' nodes. This method along with the Validate() method is called in a loop until there are no more ERROR nodes. The resulting XML document is a document conforming to its schema with all other error nodes deleted. static bool RemoveErrorNodes(XmlDocument xmlDoc) { bool deletedNodes = false; XPathNavigator xpath = xmlDoc.CreateNavigator(); XPathNodeIterator errNodes = xpath.Select("//*"); while (errNodes.MoveNext()) { XPathNodeIterator errNode = errNodes.Current.Select("*[text()='ERROR']"); while (errNode.MoveNext()) { errNode.Current.DeleteSelf(); deletedNodes = true; } } return deletedNodes; }