xsdtest.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Schema;
  5. using System.Xml.Serialization;
  6. public class Test
  7. {
  8. static char SEP = Path.DirectorySeparatorChar;
  9. public static void Main ()
  10. {
  11. RunTest ("msxsdtest");
  12. RunTest ("nisttest");
  13. RunTest ("suntest");
  14. }
  15. static void RunTest (string subdir)
  16. {
  17. Console.WriteLine ("Started: " + DateTime.Now);
  18. string basePath = @"Xsd-Test-Suite" + SEP;
  19. XmlDocument doc = new XmlDocument ();
  20. doc.Load (basePath + subdir + SEP + "tests-all.xml");
  21. foreach (XmlElement test in doc.SelectNodes ("/tests/test")) {
  22. // Test schema
  23. string schemaFile = test.SelectSingleNode ("@schema").InnerText;
  24. if (schemaFile.Length > 2)
  25. schemaFile = schemaFile.Substring (2);
  26. bool isValidSchema = test.SelectSingleNode ("@out_s").InnerText == "1";
  27. XmlSchema schema = null;
  28. try {
  29. XmlTextReader sxr = new XmlTextReader (basePath + schemaFile);
  30. Console.WriteLine ("BaseURI: " + sxr.BaseURI);
  31. schema = XmlSchema.Read (sxr, null);
  32. sxr.Close ();
  33. schema.Compile (null);
  34. if (!isValidSchema) {
  35. Console.WriteLine ("Incorrectly Valid schema : " + schemaFile);
  36. continue;
  37. }
  38. } catch (XmlSchemaException ex) {
  39. if (isValidSchema) {
  40. Console.WriteLine ("Incorrectly Invalid schema : " + schemaFile + " " + ex);
  41. continue;
  42. }
  43. } catch (Exception ex) {
  44. Console.WriteLine ("Unexpected Exception on schema: " + schemaFile + " " + ex);
  45. continue;
  46. }
  47. // Test instances
  48. string instanceFile = test.SelectSingleNode ("@instance").InnerText;
  49. if (instanceFile.Length == 0)
  50. continue;
  51. else if (instanceFile.StartsWith ("./"))
  52. instanceFile = instanceFile.Substring (2);
  53. bool isValidInstance = test.SelectSingleNode ("@out_x").InnerText == "1";
  54. try {
  55. XmlValidatingReader xvr = new XmlValidatingReader (new XmlTextReader (basePath + "\\" + instanceFile));
  56. xvr.Schemas.Add (schema);
  57. while (!xvr.EOF)
  58. xvr.Read ();
  59. if (!isValidInstance)
  60. Console.WriteLine ("Incorrectly Valid instance: " + schemaFile);
  61. xvr.Close ();
  62. } catch (XmlSchemaException ex) {
  63. if (isValidInstance)
  64. Console.WriteLine ("Incorrectly Invalid instance: " + schemaFile + " " + ex);
  65. } catch (Exception ex) {
  66. Console.WriteLine ("Unexpected Exception on instance: " + schemaFile + " " + ex);
  67. }
  68. }
  69. Console.WriteLine ("Finished: " + DateTime.Now);
  70. }
  71. }