开发者

.NET: Preventing Web Access When Validating/Reading XML Schemas?

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(Stri开发者_运维知识库ng 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜