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

| 在尝试使用XML模式验证XML文档时,我试图阻止.NET Framework访问Web,因为我不希望它一直依赖于对Web的访问。为此,我在验证时有意创建了我正在使用的所有XSD \ s的本地硬盘副本,但在加载某些这些架构时仍然失败。 例如,这段代码失败(但仅当从网络上拔下我的机器时):
using (Stream schemaStream = File.OpenRead(schemaFileName))
{
    XmlSchema schema = XmlSchema.Read(schemaStream, ValidationCallBack);
    xmlSchemaSet.Add(schema);
}
schemaFileName
指向stored2ѭ文件的本地存储副本。我得到的例外是
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)
我怀疑它仍在尝试从ѭ4加载某些内容,可能是DTD模式ѭ5。有什么办法可以防止这种情况?     
已邀请:
好吧,事实证明这比我想的要简单。这个问题解答给了我线索(并刷新了我的记忆)。 我已经有了自己的
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);                    
    }
 }
然后,我需要下载http://www.w3.org/2001/XMLSchema.dtd和http://www.w3.org/2001/datatypes.dtd的副本,现在即使没有Web访问,它也可以工作。     

要回复问题请先登录注册