validating xml file in .net2.0
-
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
-
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
-
Have you considered XmlDocument. In 2.0: XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(xmlFile); xmlDocument.Schemas.Add(xmlSchema); xmlDocument.Validate(validationHandler); Seems simpler.
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
-
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
-
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
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