public static ValidationResult Initialize(string xmlSchemaResourceName, string xmlTargetNamespace, string xmlDocumentFilename) { vResult = new ValidationResult(); XmlSchema schema; var assembly = Assembly.GetExecutingAssembly(); var stream = assembly.GetManifestResourceStream(xmlSchemaResourceName); var aimSettings = new XmlReaderSettings(); aimSettings.Schemas.Add(xmlTargetNamespace, XmlReader.Create(stream)); aimSettings.ValidationType = ValidationType.Schema; aimSettings.ValidationEventHandler += booksSettingsValidationEventHandler; var books = XmlReader.Create(xmlDocumentFilename, aimSettings); try { while (books.Read()) { } stream.Seek(0, SeekOrigin.Begin); schema = XmlSchema.Read(XmlReader.Create(stream), booksSettingsValidationEventHandler); vResult.ValidatedVersion = schema.Version; Console.WriteLine("DONE"); } catch (XmlException xmlEx) { vResult.Exception = xmlEx; Console.Write("ERROR: "); Console.WriteLine(xmlEx.Message); } if (vResult.Exception != null) vResult.Validated = false; else vResult.Validated = true; return vResult; }
public static ValidationResult Initialize(string xmlSchemaResourceName, string xmlTargetNamespace, string xmlDocumentFilename) { vResult = new ValidationResult(); XmlSchema schema; Assembly assembly = Assembly.GetExecutingAssembly(); // you can use reflector to get the full namespace of your embedded resource here Stream stream = assembly.GetManifestResourceStream(xmlSchemaResourceName); assembly.GetManifestResourceInfo(xmlSchemaResourceName); assembly.GetManifestResourceNames(); XmlSchemaSet schemas = new XmlSchemaSet(); XmlReaderSettings aimSettings = new XmlReaderSettings(); aimSettings.Schemas.Add(xmlTargetNamespace, XmlReader.Create(stream)); aimSettings.ValidationType = ValidationType.Schema; aimSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler); XmlReader books = XmlReader.Create(xmlDocumentFilename, aimSettings); try { while (books.Read()) { } // We have to read the stream.Seek(0, SeekOrigin.Begin); schema = XmlSchema.Read(XmlReader.Create(stream), booksSettingsValidationEventHandler); vResult.ValidatedVersion = schema.Version; Console.WriteLine("DONE"); } catch (XmlException xmlEx) { vResult.Exception = xmlEx; Console.Write("ERROR: "); Console.WriteLine(xmlEx.Message); } if (vResult.Exception != null) vResult.Validated = false; else vResult.Validated = true; books.Close(); return vResult; }