.NET:验证/读取 XML 模式时阻止 Web 访问?
我试图在使用 XML 架构验证 XML 文档时阻止 .NET Framework 访问 Web,因为我不希望它始终依赖于 Web 访问。为此,我特意为验证时使用的所有 XSD 创建了本地硬盘副本,但在加载某些架构时仍然失败。
例如,这段代码会失败(但前提是从网络上拔掉我的机器):
using (Stream schemaStream = File.OpenRead(schemaFileName))
{
XmlSchema schema = XmlSchema.Read(schemaStream, ValidationCallBack);
xmlSchemaSet.Add(schema);
}
schemaFileName
指向本地存储的副本xmldsig-core-schema.xsd
文件。我得到的例外是
System.Net.WebException: The remote name could not be resolved: 'www.w3.org'
Status: NameResolutionFailure
at System.Net.HttpWebRequest.GetResponse()
at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenStream(Uri uri)
at System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset(String systemId, String publicId)
at System.Xml.XmlTextReaderImpl.DtdParserProxy.System.Xml.IDtdParserAdapter.PushExternalSubset(String systemId, String publicId)
at System.Xml.DtdParser.ParseExternalSubset()
at System.Xml.DtdParser.ParseInDocumentDtd(Boolean saveInternalSubset)
at System.Xml.DtdParser.Parse(Boolean saveInternalSubset)
at System.Xml.XmlTextReaderImpl.DtdParserProxy.Parse(Boolean saveInternalSubset)
at System.Xml.XmlTextReaderImpl.ParseDoctypeDecl()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.Schema.Parser.StartParsing(XmlReader reader, String targetNamespace)
at System.Xml.Schema.Parser.Parse(XmlReader reader, String targetNamespace)
at System.Xml.Schema.XmlSchema.Read(XmlReader reader, ValidationEventHandler validationEventHandler)
at System.Xml.Schema.XmlSchema.Read(Stream stream, ValidationEventHandler validationEventHandler)
我怀疑它仍在尝试从 www.w3.org
加载某些内容,可能是 DTD 架构 http://www.w3.org/2001/XMLSchema.dtd< /代码>。 有什么办法可以防止这种情况发生吗?
I'm trying to prevent .NET Framework accessing the Web when validating an XML document using XML schemas, because I don't want it to rely on having the Web access all the time. For this purpose I intentionally created local hard disk copies of all XSD's I'm using when validating, but it still fails when loading some these schemas.
For example, this piece of code fails (but only if unplug my machine from the Web):
using (Stream schemaStream = File.OpenRead(schemaFileName))
{
XmlSchema schema = XmlSchema.Read(schemaStream, ValidationCallBack);
xmlSchemaSet.Add(schema);
}
The schemaFileName
points to a locally stored copy of xmldsig-core-schema.xsd
file. The exception I get is
System.Net.WebException: The remote name could not be resolved: 'www.w3.org'
Status: NameResolutionFailure
at System.Net.HttpWebRequest.GetResponse()
at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenStream(Uri uri)
at System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset(String systemId, String publicId)
at System.Xml.XmlTextReaderImpl.DtdParserProxy.System.Xml.IDtdParserAdapter.PushExternalSubset(String systemId, String publicId)
at System.Xml.DtdParser.ParseExternalSubset()
at System.Xml.DtdParser.ParseInDocumentDtd(Boolean saveInternalSubset)
at System.Xml.DtdParser.Parse(Boolean saveInternalSubset)
at System.Xml.XmlTextReaderImpl.DtdParserProxy.Parse(Boolean saveInternalSubset)
at System.Xml.XmlTextReaderImpl.ParseDoctypeDecl()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.Schema.Parser.StartParsing(XmlReader reader, String targetNamespace)
at System.Xml.Schema.Parser.Parse(XmlReader reader, String targetNamespace)
at System.Xml.Schema.XmlSchema.Read(XmlReader reader, ValidationEventHandler validationEventHandler)
at System.Xml.Schema.XmlSchema.Read(Stream stream, ValidationEventHandler validationEventHandler)
I suspect it's still trying to load something from www.w3.org
, possibly the DTD schema http://www.w3.org/2001/XMLSchema.dtd
. Is there any way to prevent this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,结果比我想象的要简单。这个Q/A给了我领先(并刷新了我的记忆)。
我已经有了自己的
XmlResolver
实现,用于重新路由到 XSD 文件的本地副本,但现在我还需要在加载 XML 架构时将其用于 DTD:然后我需要下载 < a href="http://www.w3.org/2001/XMLSchema.dtd" rel="nofollow noreferrer">http://www.w3.org/2001/XMLSchema.dtd 和 http://www.w3.org/2001/datatypes.dtd 现在即使没有网络访问。
Well it turned out to be simpler than I thought. This Q/A gave me the lead (and refreshed my memory).
I already have my own implementation of
XmlResolver
for rerouting to my local copies of XSD files, but now I needed to use it for DTDs when loading XML schemas, too:Then I needed to download a copy of http://www.w3.org/2001/XMLSchema.dtd and http://www.w3.org/2001/datatypes.dtd and now it works even without Web access.