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. C#
  4. validating xml file in .net2.0

validating xml file in .net2.0

Scheduled Pinned Locked Moved C#
xmlhelpcsharpdatabasequestion
5 Posts 3 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.
  • C Offline
    C Offline
    cellardoor0716
    wrote on last edited by
    #1

    I´m trying to validate a xml file against schema... I had to migrate my code from .net 1.1(where I was using XMLValidatingReader and it was working just fine...) to .net 2.0. The XMLValidatingReader became obsolete, so I tried the XMLReader instead, but I always get only the first error... Here is my sample code: private void validate() { XmlSchemaSet xsc = new XmlSchemaSet(); xsc = this.getSchemaToValidate(); XmlReaderSettings xs = new XmlReaderSettings(); xs.ValidationType = ValidationType.Schema; xs.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; xs.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); xs.Schemas.Add(xsc); fs = new FileStream("XMLTest.xml", FileMode.Open, FileAccess.Read, FileShare.Read); XmlReader vr = XmlReader.Create(fs, xs); while(vr.Read()) {} } private void ValidationCallback(object sender, ValidationEventArgs e) { tbErrorMessage += e.Message + Environment.NewLine; this.numberOfValidationErrors++; } Can anyone help me with this?

    cellardoor

    A D 2 Replies Last reply
    0
    • C cellardoor0716

      I´m trying to validate a xml file against schema... I had to migrate my code from .net 1.1(where I was using XMLValidatingReader and it was working just fine...) to .net 2.0. The XMLValidatingReader became obsolete, so I tried the XMLReader instead, but I always get only the first error... Here is my sample code: private void validate() { XmlSchemaSet xsc = new XmlSchemaSet(); xsc = this.getSchemaToValidate(); XmlReaderSettings xs = new XmlReaderSettings(); xs.ValidationType = ValidationType.Schema; xs.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; xs.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); xs.Schemas.Add(xsc); fs = new FileStream("XMLTest.xml", FileMode.Open, FileAccess.Read, FileShare.Read); XmlReader vr = XmlReader.Create(fs, xs); while(vr.Read()) {} } private void ValidationCallback(object sender, ValidationEventArgs e) { tbErrorMessage += e.Message + Environment.NewLine; this.numberOfValidationErrors++; } Can anyone help me with this?

      cellardoor

      A Offline
      A Offline
      Abisodun
      wrote on last edited by
      #2

      Have you considered XmlDocument. In 2.0: XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(xmlFile); xmlDocument.Schemas.Add(xmlSchema); xmlDocument.Validate(validationHandler); Seems simpler.

      C 1 Reply Last reply
      0
      • A Abisodun

        Have you considered XmlDocument. In 2.0: XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(xmlFile); xmlDocument.Schemas.Add(xmlSchema); xmlDocument.Validate(validationHandler); Seems simpler.

        C Offline
        C Offline
        cellardoor0716
        wrote on last edited by
        #3

        I can´t use XmlDocument because my xml files are too big(about 80MB) and I don´t want to load the whole xml into memory... I have to read them sequencially, that´s why I use XmlReader.

        cellardoor

        A 1 Reply Last reply
        0
        • C cellardoor0716

          I can´t use XmlDocument because my xml files are too big(about 80MB) and I don´t want to load the whole xml into memory... I have to read them sequencially, that´s why I use XmlReader.

          cellardoor

          A Offline
          A Offline
          Abisodun
          wrote on last edited by
          #4

          I can't tell you - tried a quick test with code identical to yours except that I used Schema rather than schema set and got multiple errors. Good Luck!

          1 Reply Last reply
          0
          • C cellardoor0716

            I´m trying to validate a xml file against schema... I had to migrate my code from .net 1.1(where I was using XMLValidatingReader and it was working just fine...) to .net 2.0. The XMLValidatingReader became obsolete, so I tried the XMLReader instead, but I always get only the first error... Here is my sample code: private void validate() { XmlSchemaSet xsc = new XmlSchemaSet(); xsc = this.getSchemaToValidate(); XmlReaderSettings xs = new XmlReaderSettings(); xs.ValidationType = ValidationType.Schema; xs.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; xs.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); xs.Schemas.Add(xsc); fs = new FileStream("XMLTest.xml", FileMode.Open, FileAccess.Read, FileShare.Read); XmlReader vr = XmlReader.Create(fs, xs); while(vr.Read()) {} } private void ValidationCallback(object sender, ValidationEventArgs e) { tbErrorMessage += e.Message + Environment.NewLine; this.numberOfValidationErrors++; } Can anyone help me with this?

            cellardoor

            D Offline
            D Offline
            DavidNohejl
            wrote on last edited by
            #5

            It looks very similar like examples from MSDN. I am surprised it doesn't work.... I don't really have much experience with "new" xml classes in .NET 2.0 , but did you consider XmlSchemaValidator[^]?


            "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

            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