Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. How to handle fixing XML validation errors in C#.NET?

How to handle fixing XML validation errors in C#.NET?

Scheduled Pinned Locked Moved XML / XSL
csharpxmltutorialquestiondatabase
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ritatx25
    wrote on last edited by
    #1

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

    L 1 Reply Last reply
    0
    • R ritatx25

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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • L led mike

        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

        R Offline
        R Offline
        ritatx25
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups