Ser*_*amP 0 .net wpf xsd exception
我在 VB.NET 中工作 - C# 答案很好。
我正在构建一个系统来处理针对特定 XmlSchema 的 XML 验证。我的 XML 文档中的每个错误都有一个 SchemaException 实例,我可以从中获取错误的行号和位置以及消息。据我所知,确定特定错误(无效属性、缺少元素等)的唯一方法是读取不可靠的消息字符串,因为 MS 将来可能更改它并且消息被本地化。
我需要能够在不依赖 .Message 属性的情况下区分这些错误,以便显示我自己的自定义错误并在我的文本编辑器中突出显示错误。
区分这些的正确方法是什么?这一定是可能的,对吧?
更多信息:
异常的 LinePosition 属性并不总是报告我想要开始突出显示的位置 - 例如,所有与属性相关的异常都会报告属性的开始,如果这是问题。
SchemaException 提供了一个 SourceSchemaObject 属性,我可能可以用它来确定问题是来自元素还是属性,如果幸运的话,我可能会通过提取导致错误并以某种方式将它与 SourceSchemaObject 进行比较,但这感觉像是一个非常复杂和笨拙的解决方案 - 如果我能解决特定的错误,我可以通过一些正则表达式工作来解决它。
好的,我最终解决了这个问题。我认为异常必须具有指示特定错误的内部值才能生成消息,因此我使用 ILSpy 深入观察它的灵魂。事实证明,它确实有一个名为“res”的内部字段,其中包含一个字符串,该字符串映射到我们可以使用反射访问的错误。
我写了一个小扩展方法,它为我检索这个错误作为一个字符串。
<Extension()>
Public Function getErrorType(ByRef ref As XmlSchemaException) As XmlSchemaExceptionErrorType
Return GetType(XmlSchemaException).GetField("res", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance).GetValue(ref)
End Function
Run Code Online (Sandbox Code Playgroud)
这很棒,但我不想为 Select Case 场景记住所有这些字符串,所以我编写了一个脚本来将我使用 ILSpy 提取的源转换为 Enum 并重新编写扩展方法,如下所示:
<Extension()>
Public Function getErrorType(ByRef ref As XmlSchemaException) As XmlSchemaExceptionErrorType
Dim res As String = GetType(XmlSchemaException).GetField("res", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance).GetValue(ref)
Return CType(System.Enum.Parse(GetType(XmlSchemaExceptionErrorType), res), XmlSchemaExceptionErrorType)
End Function
Public Enum XmlSchemaExceptionErrorType As Integer
Sch_ParEntityRefNesting = 0
Sch_NotTokenString = 1
Sch_XsdDateTimeCompare = 2
Sch_InvalidNullCast = 3
Sch_InvalidDateTimeOption = 4
Sch_StandAloneNormalization = 5
Sch_UnSpecifiedDefaultAttributeInExternalStandalone = 6
Sch_DefaultException = 7
Sch_DupElementDecl = 8
Sch_IdAttrDeclared = 9
Sch_RootMatchDocType = 10
Sch_DupId = 11
Sch_UndeclaredElement = 12
Sch_UndeclaredAttribute = 13
Sch_UndeclaredNotation = 14
Sch_UndeclaredId = 15
Sch_SchemaRootExpected = 16
Sch_XSDSchemaRootExpected = 17
Sch_UnsupportedAttribute = 18
Sch_UnsupportedElement = 19
Sch_MissAttribute = 20
Sch_AnnotationLocation = 21
Sch_DataTypeTextOnly = 22
Sch_UnknownModel = 23
Sch_UnknownOrder = 24
Sch_UnknownContent = 25
Sch_UnknownRequired = 26
Sch_UnknownDtType = 27
Sch_MixedMany = 28
Sch_GroupDisabled = 29
Sch_MissDtvalue = 30
Sch_MissDtvaluesAttribute = 31
Sch_DupDtType = 32
Sch_DupAttribute = 33
Sch_RequireEnumeration = 34
Sch_DefaultIdValue = 35
Sch_ElementNotAllowed = 36
Sch_ElementMissing = 37
Sch_ManyMaxOccurs = 38
Sch_MaxOccursInvalid = 39
Sch_MinOccursInvalid = 40
Sch_DtMaxLengthInvalid = 41
Sch_DtMinLengthInvalid = 42
Sch_DupDtMaxLength = 43
Sch_DupDtMinLength = 44
Sch_DtMinMaxLength = 45
Sch_DupElement = 46
Sch_DupGroupParticle = 47
Sch_InvalidValue = 48
Sch_InvalidValueDetailed = 49
Sch_InvalidValueDetailedAttribute = 50
Sch_MissRequiredAttribute = 51
Sch_FixedAttributeValue = 52
Sch_FixedElementValue = 53
Sch_AttributeValueDataTypeDetailed = 54
Sch_AttributeDefaultDataType = 55
Sch_IncludeLocation = 56
Sch_ImportLocation = 57
Sch_RedefineLocation = 58
Sch_InvalidBlockDefaultValue = 59
Sch_InvalidFinalDefaultValue = 60
Sch_InvalidElementBlockValue = 61
Sch_InvalidElementFinalValue = 62
Sch_InvalidSimpleTypeFinalValue = 63
Sch_InvalidComplexTypeBlockValue = 64
Sch_InvalidComplexTypeFinalValue = 65
Sch_DupIdentityConstraint = 66
Sch_DupGlobalElement = 67
Sch_DupGlobalAttribute = 68
Sch_DupSimpleType = 69
Sch_DupComplexType = 70
Sch_DupGroup = 71
Sch_DupAttributeGroup = 72
Sch_DupNotation = 73
Sch_DefaultFixedAttributes = 74
Sch_FixedInRef = 75
Sch_FixedDefaultInRef = 76
Sch_DupXsdElement = 77
Sch_ForbiddenAttribute = 78
Sch_AttributeIgnored = 79
Sch_ElementRef = 80
Sch_TypeMutualExclusive = 81
Sch_ElementNameRef = 82
Sch_AttributeNameRef = 83
Sch_TextNotAllowed = 84
Sch_UndeclaredType = 85
Sch_UndeclaredSimpleType = 86
Sch_UndeclaredEquivClass = 87
Sch_AttListPresence = 88
Sch_NotationValue = 89
Sch_EnumerationValue = 90
Sch_EmptyAttributeValue = 91
Sch_InvalidLanguageId = 92
Sch_XmlSpace = 93
Sch_InvalidXsdAttributeValue = 94
Sch_InvalidXsdAttributeDatatypeValue = 95
Sch_ElementValueDataTypeDetailed = 96
Sch_InvalidElementDefaultValue = 97
Sch_NonDeterministic = 98
Sch_NonDeterministicAnyEx = 99
Sch_NonDeterministicAnyAny = 100
Sch_StandAlone = 101
Sch_XmlNsAttribute = 102
Sch_AllElement = 103
Sch_MismatchTargetNamespaceInclude = 104
Sch_MismatchTargetNamespaceImport = 105
Sch_MismatchTargetNamespaceEx = 106
Sch_XsiTypeNotFound = 107
Sch_XsiTypeAbstract = 108
Sch_ListFromNonatomic = 109
Sch_UnionFromUnion = 110
Sch_DupLengthFacet = 111
Sch_DupMinLengthFacet = 112
Sch_DupMaxLengthFacet = 113
Sch_DupWhiteSpaceFacet = 114
Sch_DupMaxInclusiveFacet = 115
Sch_DupMaxExclusiveFacet = 116
Sch_DupMinInclusiveFacet = 117
Sch_DupMinExclusiveFacet = 118
Sch_DupTotalDigitsFacet = 119
Sch_DupFractionDigitsFacet = 120
Sch_LengthFacetProhibited = 121
Sch_MinLengthFacetProhibited = 122
Sch_MaxLengthFacetProhibited = 123
Sch_PatternFacetProhibited = 124
Sch_EnumerationFacetProhibited = 125
Sch_WhiteSpaceFacetProhibited = 126
Sch_MaxInclusiveFacetProhibited = 127
Sch_MaxExclusiveFacetProhibited = 128
Sch_MinInclusiveFacetProhibited = 129
Sch_MinExclusiveFacetProhibited = 130
Sch_TotalDigitsFacetProhibited = 131
Sch_FractionDigitsFacetProhibited = 132
Sch_LengthFacetInvalid = 133
Sch_MinLengthFacetInvalid = 134
Sch_MaxLengthFacetInvalid = 135
Sch_MaxInclusiveFacetInvalid = 136
Sch_MaxExclusiveFacetInvalid = 137
Sch_MinInclusiveFacetInvalid = 138
Sch_MinExclusiveFacetInvalid = 139
Sch_TotalDigitsFacetInvalid = 140
Sch_FractionDigitsFacetInvalid = 141
Sch_PatternFacetInvalid = 142
Sch_EnumerationFacetInvalid = 143
Sch_InvalidWhiteSpace = 144
Sch_UnknownFacet = 145
Sch_LengthAndMinMax = 146
Sch_MinLengthGtMaxLength = 147
Sch_FractionDigitsGtTotalDigits = 148
Sch_LengthConstraintFailed = 149
Sch_MinLengthConstraintFailed = 150
Sch_MaxLengthConstraintFailed = 151
Sch_PatternConstraintFailed = 152
Sch_EnumerationConstraintFailed = 153
Sch_MaxInclusiveConstraintFailed = 154
Sch_MaxExclusiveConstraintFailed = 155
Sch_MinInclusiveConstraintFailed = 156
Sch_MinExclusiveConstraintFailed = 157
Sch_TotalDigitsConstraintFailed = 158
Sch_FractionDigitsConstraintFailed = 159
Sch_UnionFailedEx = 160
Sch_NotationRequired = 161
Sch_DupNotationAttribute = 162
Sch_MissingPublicSystemAttribute = 163
Sch_NotationAttributeOnEmptyElement = 164
Sch_RefNotInScope = 165
Sch_UndeclaredIdentityConstraint = 166
Sch_RefInvalidIdentityConstraint = 167
Sch_RefInvalidCardin = 168
Sch_ReftoKeyref = 169
Sch_EmptyXPath = 170
Sch_UnresolvedPrefix = 171
Sch_UnresolvedKeyref = 172
Sch_ICXpathError = 173
Sch_SelectorAttr = 174
Sch_FieldSimpleTypeExpected = 175
Sch_FieldSingleValueExpected = 176
Sch_MissingKey = 177
Sch_DuplicateKey = 178
Sch_TargetNamespaceXsi = 179
Sch_UndeclaredEntity = 180
Sch_UnparsedEntityRef = 181
Sch_MaxOccursInvalidXsd = 182
Sch_MinOccursInvalidXsd = 183
Sch_MaxInclusiveExclusive = 184
Sch_MinInclusiveExclusive = 185
Sch_MinInclusiveGtMaxInclusive = 186
Sch_MinExclusiveGtMaxExclusive = 187
Sch_MinInclusiveGtMaxExclusive = 188
Sch_MinExclusiveGtMaxInclusive = 189
Sch_SimpleTypeRestriction = 190
Sch_InvalidFacetPosition = 191
Sch_AttributeMutuallyExclusive = 192
Sch_AnyAttributeLastChild = 193
Sch_ComplexTypeContentModel = 194
Sch_ComplexContentContentModel = 195
Sch_NotNormalizedString = 196
Sch_FractionDigitsNotOnDecimal = 197
Sch_ContentInNill = 198
Sch_NoElementSchemaFound = 199
Sch_NoAttributeSchemaFound = 200
Sch_InvalidNamespace = 201
Sch_InvalidTargetNamespaceAttribute = 202
Sch_InvalidNamespaceAttribute = 203
Sch_InvalidSchemaLocation = 204
Sch_ImportTargetNamespace = 205
Sch_ImportTargetNamespaceNull = 206
Sch_GroupDoubleRedefine = 207
Sch_ComponentRedefineNotFound = 208
Sch_GroupRedefineNotFound = 209
Sch_AttrGroupDoubleRedefine = 210
Sch_AttrGroupRedefineNotFound = 211
Sch_ComplexTypeDoubleRedefine = 212
Sch_ComplexTypeRedefineNotFound = 213
Sch_SimpleToComplexTypeRedefine = 214
Sch_SimpleTypeDoubleRedefine = 215
Sch_ComplexToSimpleTypeRedefine = 216
Sch_SimpleTypeRedefineNotFound = 217
Sch_MinMaxGroupRedefine = 218
Sch_MultipleGroupSelfRef = 219
Sch_MultipleAttrGroupSelfRef = 220
Sch_InvalidTypeRedefine = 221
Sch_InvalidElementRef = 222
Sch_MinGtMax = 223
Sch_DupSelector = 224
Sch_IdConstraintNoSelector = 225
Sch_IdConstraintNoFields = 226
Sch_IdConstraintNoRefer = 227
Sch_SelectorBeforeFields = 228
Sch_NoSimpleTypeContent = 229
Sch_SimpleTypeRestRefBase = 230
Sch_SimpleTypeRestRefBaseNone = 231
Sch_SimpleTypeListRefBase = 232
Sch_SimpleTypeListRefBaseNone = 233
Sch_SimpleTypeUnionNoBase = 234
Sch_NoRestOrExtQName = 235
Sch_NoRestOrExt = 236
Sch_NoGroupParticle = 237
Sch_InvalidAllMin = 238
Sch_InvalidAllMax = 239
Sch_InvalidFacet = 240
Sch_AbstractElement = 241
Sch_XsiTypeBlockedEx = 242
Sch_InvalidXsiNill = 243
Sch_SubstitutionNotAllowed = 244
Sch_SubstitutionBlocked = 245
Sch_InvalidElementInEmptyEx = 246
Sch_InvalidElementInTextOnlyEx = 247
Sch_InvalidTextInElement = 248
Sch_InvalidElementContent = 249
Sch_InvalidElementContentComplex = 250
Sch_IncompleteContent = 251
Sch_IncompleteContentComplex = 252
Sch_InvalidTextInElementExpecting = 253
Sch_InvalidElementContentExpecting = 254
Sch_InvalidElementContentExpectingComplex = 255
Sch_IncompleteContentExpecting = 256
Sch_IncompleteContentExpectingComplex = 257
Sch_InvalidElementSubstitution = 258
Sch_ElementNameAndNamespace = 259
Sch_ElementName = 260
Sch_ContinuationString = 261
Sch_AnyElementNS = 262
Sch_AnyElement = 263
Sch_InvalidTextInEmpty = 264
Sch_InvalidWhitespaceInEmpty = 265
Sch_InvalidPIComment = 266
Sch_InvalidAttributeRef = 267
Sch_OptionalDefaultAttribute = 268
Sch_AttributeCircularRef = 269
Sch_IdentityConstraintCircularRef = 270
Sch_SubstitutionCircularRef = 271
Sch_InvalidAnyAttribute = 272
Sch_DupIdAttribute = 273
Sch_InvalidAllElementMax = 274
Sch_InvalidAny = 275
Sch_InvalidAnyDetailed = 276
Sch_InvalidExamplar = 277
Sch_NoExamplar = 278
Sch_InvalidSubstitutionMember = 279
Sch_RedefineNoSchema = 280
Sch_ProhibitedAttribute = 281
Sch_TypeCircularRef = 282
Sch_TwoIdAttrUses = 283
Sch_AttrUseAndWildId = 284
Sch_MoreThanOneWildId = 285
Sch_BaseFinalExtension = 286
Sch_NotSimpleContent = 287
Sch_NotComplexContent = 288
Sch_BaseFinalRestriction = 289
Sch_BaseFinalList = 290
Sch_BaseFinalUnion = 291
Sch_UndefBaseRestriction = 292
Sch_UndefBaseExtension = 293
Sch_DifContentType = 294
Sch_InvalidContentRestriction = 295
Sch_InvalidContentRestrictionDetailed = 296
Sch_InvalidBaseToEmpty = 297
Sch_InvalidBaseToMixed = 298
Sch_DupAttributeUse = 299
Sch_InvalidParticleRestriction = 300
Sch_InvalidParticleRestrictionDetailed = 301
Sch_ForbiddenDerivedParticleForAll = 302
Sch_ForbiddenDerivedParticleForElem = 303
Sch_ForbiddenDerivedParticleForChoice = 304
Sch_ForbiddenDerivedParticleForSeq = 305
Sch_ElementFromElement = 306
Sch_ElementFromAnyRule1 = 307
Sch_ElementFromAnyRule2 = 308
Sch_AnyFromAnyRule1 = 309
Sch_AnyFromAnyRule2 = 310
Sch_AnyFromAnyRule3 = 311
Sch_GroupBaseFromAny1 = 312
Sch_GroupBaseFromAny2 = 313
Sch_ElementFromGroupBase1 = 314
Sch_ElementFromGroupBase2 = 315
Sch_ElementFromGroupBase3 = 316
Sch_GroupBaseRestRangeInvalid = 317
Sch_GroupBaseRestNoMap = 318
Sch_GroupBaseRestNotEmptiable = 319
Sch_SeqFromAll = 320
Sch_SeqFromChoice = 321
Sch_UndefGroupRef = 322
Sch_GroupCircularRef = 323
Sch_AllRefNotRoot = 324
Sch_AllRefMinMax = 325
Sch_NotAllAlone = 326
Sch_AttributeGroupCircularRef = 327
Sch_UndefAttributeGroupRef = 328
Sch_InvalidAttributeExtension = 329
Sch_InvalidAnyAttributeRestriction = 330
Sch_AttributeRestrictionProhibited = 331
Sch_AttributeRestrictionInvalid = 332
Sch_AttributeFixedInvalid = 333
Sch_AttributeUseInvalid = 334
Sch_AttributeRestrictionInvalidFromWildcard = 335
Sch_NoDerivedAttribute = 336
Sch_UnexpressibleAnyAttribute = 337
Sch_RefInvalidAttribute = 338
Sch_ElementCircularRef = 339
Sch_RefInvalidElement = 340
Sch_ElementCannotHaveValue = 341
Sch_ElementInMixedWithFixed = 342
Sch_ElementTypeCollision = 343
Sch_InvalidIncludeLocation = 344
Sch_CannotLoadSchema = 345
Sch_CannotLoadSchemaLocation = 346
Sch_LengthGtBaseLength = 347
Sch_MinLengthGtBaseMinLength = 348
Sch_MaxLengthGtBaseMaxLength = 349
Sch_MaxMinLengthBaseLength = 350
Sch_MaxInclusiveMismatch = 351
Sch_MaxExclusiveMismatch = 352
Sch_MinInclusiveMismatch = 353
Sch_MinExclusiveMismatch = 354
Sch_MinExlIncMismatch = 355
Sch_MinExlMaxExlMismatch = 356
Sch_MinIncMaxExlMismatch = 357
Sch_MinIncExlMismatch = 358
Sch_MaxIncExlMismatch = 359
Sch_MaxExlIncMismatch = 360
Sch_TotalDigitsMismatch = 361
Sch_FacetBaseFixed = 362
Sch_WhiteSpaceRestriction1 = 363
Sch_WhiteSpaceRestriction2 = 364
Sch_XsiNilAndFixed = 365
Sch_MixSchemaTypes = 366
Sch_XSDSchemaOnly = 367
Sch_InvalidPublicAttribute = 368
Sch_InvalidSystemAttribute = 369
Sch_TypeAfterConstraints = 370
Sch_XsiNilAndType = 371
Sch_DupSimpleTypeChild = 372
Sch_InvalidIdAttribute = 373
Sch_InvalidNameAttributeEx = 374
Sch_InvalidAttribute = 375
Sch_EmptyChoice = 376
Sch_DerivedNotFromBase = 377
Sch_NeedSimpleTypeChild = 378
Sch_InvalidCollection = 379
Sch_UnrefNS = 380
Sch_InvalidSimpleTypeRestriction = 381
Sch_MultipleRedefine = 382
Sch_NullValue = 383
Sch_ComplexContentModel = 384
Sch_SchemaNotPreprocessed = 385
Sch_SchemaNotRemoved = 386
Sch_ComponentAlreadySeenForNS = 387
Sch_DefaultAttributeNotApplied = 388
Sch_NotXsiAttribute = 389
Sch_SchemaDoesNotExist = 390
Sch_InvalidStartTransition = 391
Sch_InvalidStateTransition = 392
Sch_InvalidEndValidation = 393
Sch_InvalidEndElementCall = 394
Sch_InvalidEndElementCallTyped = 395
Sch_InvalidEndElementMultiple = 396
Sch_DuplicateAttribute = 397
Sch_InvalidPartialValidationType = 398
Sch_SchemaElementNameMismatch = 399
Sch_SchemaAttributeNameMismatch = 400
Sch_ValidateAttributeInvalidCall = 401
Sch_ValidateElementInvalidCall = 402
Sch_EnumNotStarted = 403
Sch_EnumFinished = 404
Sch_ErrorPosition = 405
Sch_ReservedNsDecl = 406
Sch_NotInSchemaCollection = 407
Sch_NotationNotAttr = 408
Sch_InvalidContent = 409
Sch_InvalidContentExpecting = 410
Sch_InvalidTextWhiteSpace = 411
Sch_XSCHEMA = 412
Sch_DubSchema = 413
Sch_AttributeValueDataType = 414
Sch_ElementValueDataType = 415
Sch_NonDeterministicAny = 416
Sch_MismatchTargetNamespace = 417
Sch_UnionFailed = 418
Sch_XsiTypeBlocked = 419
Sch_InvalidElementInEmpty = 420
Sch_InvalidElementInTextOnly = 421
Sch_InvalidNameAttribute = 422
End Enum
Run Code Online (Sandbox Code Playgroud)
哒哒!
现在我可以像这样进行错误处理:
Select Case e.Exception.getErrorType
Case XmlSchemaExceptionErrorType.Sch_UndeclaredAttribute
' Error specific code
Case XmlSchemaExceptionErrorType.Sch_UndeclaredElement
' Error specific code
Case Else
' General code for handling any other errors we don't specifically care about
End Select
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
962 次 |
| 最近记录: |