.NET:验证/读取 XML 模式时阻止 Web 访问?

发布于 2024-10-31 22:51:56 字数 2140 浏览 3 评论 0原文

我试图在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小姐丶请自重 2024-11-07 22:51:56

好吧,结果比我想象的要简单。这个Q/A给了我领先(并刷新了我的记忆)。

我已经有了自己的 XmlResolver 实现,用于重新路由到 XSD 文件的本地副本,但现在我还需要在加载 XML 架构时将其用于 DTD:

using (Stream schemaStream = File.OpenRead(schemaFileName))
{
    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
    xmlReaderSettings.XmlResolver = myXmlNamespaceResolver;
    xmlReaderSettings.ProhibitDtd = false;

    using (XmlReader reader = XmlReader.Create(schemaStream, xmlReaderSettings))
    {
        XmlSchema schema = XmlSchema.Read(reader, ValidationCallBack);
        xmlSchemaSet.Add(schema);                    
    }
 }

然后我需要下载 < 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:

using (Stream schemaStream = File.OpenRead(schemaFileName))
{
    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
    xmlReaderSettings.XmlResolver = myXmlNamespaceResolver;
    xmlReaderSettings.ProhibitDtd = false;

    using (XmlReader reader = XmlReader.Create(schemaStream, xmlReaderSettings))
    {
        XmlSchema schema = XmlSchema.Read(reader, ValidationCallBack);
        xmlSchemaSet.Add(schema);                    
    }
 }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文