我正在尝试从包URI添加到XMLSchemaSet,我收到以下错误:
抛出异常:System.dll中的“System.NotSupportedException”
其他信息:无法识别URI前缀。
以下是代码:
Dim XML = XDocument.Parse("<Product></Product>")
Dim Schemas As New XmlSchemaSet()
Schemas.Add("", "pack://application:,,,/MyAssemblyName;component/Resources/XSD Resources/Product.xsd")
XML.Validate(Schemas, Sub(sender As Object, e As ValidationEventArgs) Console.WriteLine(e.Message))
从本地路径加载时没有任何问题,但是这些XSD需要与应用程序一起打包。我在这里对包URI做错了什么?
答案 0 :(得分:0)
您可以指定自定义XmlResolver
并提供一个知道如何加载&#34; pack:&#34;的实现。的URI:
Dim XML = XDocument.Parse("<Product></Product>")
Dim Schemas As New XmlSchemaSet()
Schemas.XmlResolver = New CustomResolver()
Schemas.Add("", "pack://application:,,,/MyAssemblyName;component/Resources/XSD Resources/Product.xsd")
XML.Validate(Schemas, Sub(sender As Object, e As ValidationEventArgs) Console.WriteLine(e.Message))
' ...
Class CustomResolver
Inherits XmlUrlResolver
Public Overrides Function GetEntity(absoluteUri As Uri, role As String, ofObjectToReturn As Type) As Object
If absoluteUri.Scheme = "pack" Then
Return Application.GetResourceStream(absoluteUri).Stream
End If
Return MyBase.GetEntity(absoluteUri, role, ofObjectToReturn)
End Function
End Class
这只是经过最低限度的测试,因此您可能需要添加一些错误处理等等。