XmlSchemaBase.cs
上传用户:husern
上传日期:2022-03-24
资源大小:534k
文件大小:5k
源码类别:

编辑器/阅读器

开发平台:

C#

  1. // -- FILE ------------------------------------------------------------------
  2. // name       : XmlSchemaBase.cs
  3. // project    : Itenso Web User Forms
  4. // created    : Jani Giannoudis - 2008.10.30
  5. // language   : c#
  6. // environment: .NET 2.0
  7. // copyright  : (c) 2008 by Itenso GmbH, Switzerland
  8. // --------------------------------------------------------------------------
  9. using System;
  10. using System.IO;
  11. using System.Xml;
  12. using System.Xml.Schema;
  13. using System.Text;
  14. using System.Diagnostics;
  15. using System.Globalization;
  16. namespace Itenso.WebUserForms.Data
  17. {
  18. // ------------------------------------------------------------------------
  19. public abstract class XmlSchemaBase
  20. {
  21. // ----------------------------------------------------------------------
  22. public const string XsdPrefix = "xsd";
  23. public const string XsdSuffix = ".xsd";
  24. // ----------------------------------------------------------------------
  25. public static XmlSchema LoadSchema( Type type )
  26. {
  27. return LoadSchema( type, type.Name + XsdSuffix );
  28. } // LoadSchema
  29. // ----------------------------------------------------------------------
  30. public static XmlSchema LoadSchema( Type type, string resourceName )
  31. {
  32. Stream schemaStream = ResourceTool.GetResourceAsStream( type, resourceName );
  33. return XmlSchema.Read( schemaStream, XmlSchemaValidationHandler );
  34. } // LoadSchema
  35. // ----------------------------------------------------------------------
  36. public static XmlReaderSettings CreateStrictSchemaValidationSettings()
  37. {
  38. return CreateStrictSchemaValidationSettings( null );
  39. } // CreateStrictSchemaValidationSettings
  40. // ----------------------------------------------------------------------
  41. public static XmlReaderSettings CreateStrictSchemaValidationSettings( params XmlSchema[] schemas )
  42. {
  43. XmlReaderSettings settings = new XmlReaderSettings();
  44. if ( schemas != null )
  45. {
  46. for ( int i = 0; i < schemas.Length; i++ )
  47. {
  48. XmlSchema schema = schemas[ i ];
  49. if ( schema != null )
  50. {
  51. settings.Schemas.Add( schema );
  52. }
  53. }
  54. }
  55. settings.ValidationFlags =
  56. //XmlSchemaValidationFlags.ProcessInlineSchema |
  57. //XmlSchemaValidationFlags.ProcessSchemaLocation |
  58. XmlSchemaValidationFlags.ProcessIdentityConstraints |
  59. XmlSchemaValidationFlags.ReportValidationWarnings;
  60. settings.ValidationType = ValidationType.Schema;
  61. settings.ValidationEventHandler += XmlSchemaValidationWarningsAsExceptionsHandler;
  62. settings.ValidationEventHandler += XmlSchemaValidationErrorsAsExceptionsHandler;
  63. settings.ConformanceLevel = ConformanceLevel.Fragment;
  64. return settings;
  65. } // CreateStrictSchemaValidationSettings
  66. // ----------------------------------------------------------------------
  67. public static void XmlSchemaValidationHandler( object sender, ValidationEventArgs e )
  68. {
  69. if ( e.Severity == XmlSeverityType.Warning )
  70. {
  71. Debug.Fail( "XML Schema validation warning", e.Message );
  72. }
  73. else if ( e.Severity == XmlSeverityType.Error )
  74. {
  75. Debug.Fail( "XML Schema validation error", e.Message );
  76. }
  77. } // XmlSchemaValidationHandler
  78. // ----------------------------------------------------------------------
  79. public static void XmlSchemaValidationWarningsAsExceptionsHandler( object sender, ValidationEventArgs e )
  80. {
  81. if ( e.Severity == XmlSeverityType.Warning )
  82. {
  83. ThrowXmlExceptionFor( "XML Schema validation warning: {0}", e.Message, e.Exception );
  84. }
  85. } // XmlSchemaValidationWarningsAsExceptionsHandler
  86. // ----------------------------------------------------------------------
  87. public static void XmlSchemaValidationErrorsAsExceptionsHandler( object sender, ValidationEventArgs e )
  88. {
  89. if ( e.Severity == XmlSeverityType.Error )
  90. {
  91. ThrowXmlExceptionFor( "XML Schema validation error: {0}", e.Message, e.Exception );
  92. }
  93. } // XmlSchemaValidationErrorsAsExceptionsHandler
  94. // ----------------------------------------------------------------------
  95. private static void ThrowXmlExceptionFor( string message, string error, Exception e )
  96. {
  97. string exceptionText = String.Format( CultureInfo.InvariantCulture, message, error );
  98. XmlException xmlExc = e as XmlException;
  99. XmlSchemaException schemaExc = e as XmlSchemaException;
  100. if ( xmlExc != null )
  101. {
  102. throw new XmlException( exceptionText, e, xmlExc.LineNumber, xmlExc.LinePosition );
  103. }
  104. else if ( schemaExc != null )
  105. {
  106. throw new XmlException( exceptionText, e, schemaExc.LineNumber, schemaExc.LinePosition );
  107. }
  108. else if ( e != null )
  109. {
  110. throw new XmlException( exceptionText, e );
  111. }
  112. else
  113. {
  114. throw new XmlException( exceptionText );
  115. }
  116. } // ThrowXmlExceptionFor
  117. } // class XmlSchemaBase
  118. } // namespace Itenso.WebUserForms.Data
  119. // -- EOF -------------------------------------------------------------------