c# and XML validation [modified]
-
Hi there, I have created an .xsd schema which I want to include in my project (Windows Service). First question is: what is the best way to include it? as a resource? or just by doing "Add existing item"? what is the difference(if any)? So far, I have added it as a resource (jobs.xsd). Ok, so now I want to validate an xml file, thus I am trying to do as follows:
1. XmlSchemaSet xss = new XmlSchemaSet();
2. //Add the schema to the collection
3. xss.Add("http://www.w3.org/2001/XMLSchema",ClassLibrary1.Properties.Resources.jobs);
4. XmlReaderSettings settings = new XmlReaderSettings();
5. settings.ValidationType = ValidationType.Schema;
6. settings.Schemas = xss;
7. settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
8. // Create the XmlReader object.
9. XmlReader reader = XmlReader.Create(xmlPath, settings);
10. // Parse the file.
11. while (reader.Read()){};This code breaks on line 3, with a System.ArgumentException: Illegal characters in path. at System.IO.Path.CheckInvalidPathChars(String path) at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck) at System.IO.Path.GetFullPathInternal(String path) at System.IO.Path.GetFullPath(String path) at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri) at System.Xml.XmlUrlResolver.ResolveUri(Uri baseUri, String relativeUri) at System.Xml.Schema.XmlSchemaSet.Add(String targetNamespace, String schemaUri) at ClassLibrary1.ReadXml.ValidateXMLAgainstXSD(String xmlPath) in c:\myproj\ClassLibrary1\GetXml.cs:line 95} How could I get around this problem? Cheers
modified on Tuesday, March 3, 2009 6:57 AM
-
Hi there, I have created an .xsd schema which I want to include in my project (Windows Service). First question is: what is the best way to include it? as a resource? or just by doing "Add existing item"? what is the difference(if any)? So far, I have added it as a resource (jobs.xsd). Ok, so now I want to validate an xml file, thus I am trying to do as follows:
1. XmlSchemaSet xss = new XmlSchemaSet();
2. //Add the schema to the collection
3. xss.Add("http://www.w3.org/2001/XMLSchema",ClassLibrary1.Properties.Resources.jobs);
4. XmlReaderSettings settings = new XmlReaderSettings();
5. settings.ValidationType = ValidationType.Schema;
6. settings.Schemas = xss;
7. settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
8. // Create the XmlReader object.
9. XmlReader reader = XmlReader.Create(xmlPath, settings);
10. // Parse the file.
11. while (reader.Read()){};This code breaks on line 3, with a System.ArgumentException: Illegal characters in path. at System.IO.Path.CheckInvalidPathChars(String path) at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck) at System.IO.Path.GetFullPathInternal(String path) at System.IO.Path.GetFullPath(String path) at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri) at System.Xml.XmlUrlResolver.ResolveUri(Uri baseUri, String relativeUri) at System.Xml.Schema.XmlSchemaSet.Add(String targetNamespace, String schemaUri) at ClassLibrary1.ReadXml.ValidateXMLAgainstXSD(String xmlPath) in c:\myproj\ClassLibrary1\GetXml.cs:line 95} How could I get around this problem? Cheers
modified on Tuesday, March 3, 2009 6:57 AM
If you still have not figured it out, the .Add overload you're using expects the URi of your .xsd file, and not the file's contents as a string. It compiles, because there is not type mismatch, obviously. All you can do is stream the contents of your .xsd, then feed it to an XmlReader and then pass that XmlReader as your second parameter of .Add's next overload. That should help, hopefully.
var question = (_2b || !(_2b));
-
If you still have not figured it out, the .Add overload you're using expects the URi of your .xsd file, and not the file's contents as a string. It compiles, because there is not type mismatch, obviously. All you can do is stream the contents of your .xsd, then feed it to an XmlReader and then pass that XmlReader as your second parameter of .Add's next overload. That should help, hopefully.
var question = (_2b || !(_2b));
Hi Greg, I think I should have been more precise. I knew I was passing the wrong data type to the .Add method. My problem was converting my string into a valid data type that the .Add will take. At the end I am doing as follows:
StringReader sr = new StringReader(ClassLibrary1.Properties.Resources.jobs);
XmlSchema schema = XmlSchema.Read(sr,null);
sr.Close();
//Add the schema to the collection
xss.Add(schema);Anyway, thanks for your post!