在我们的C#
应用程序中,有时应用程序需要打开一个 svg 文件。为此,使用以下代码:
XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.None;
XmlReader xr = XmlReader.Create(serverMainPath + @"/path/to/svg/file.svg", settings);
XmlDocument xd = new XmlDocument();
_nmsm = new XmlNamespaceManager(xd.NameTable);
_nmsm.AddNamespace("svg", "http://www.w3.org/2000/svg");
_nmsm.AddNamespace("v", "http://schemas.microsoft.com/visio/2003/SVGExtensions/");
xd.Load(xr);
我们使用的标准 svg 以(据我所知它是一个有效的 svg)开头:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<!-- Généré par Microsoft Visio 11.0, SVG Export, v1.0 svgIncluded.svg Page-1 -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:v="http://schemas.microsoft.com/visio/2003/SVGExtensions/" width="8.26772in" height="11.6929in" viewBox="0 0 595.276 841.889" xml:space="preserve" color-interpolation-filters="sRGB" class="st23" >
在过去的 6 个月里,它工作得很好。但是从一两周开始,这段代码有时会产生以下错误:
Server Error in '/ourApp' Application.
--------------------------------------------------------------------------------
The remote server returned an error: (403) Forbidden.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.WebException: The remote server returned an error: (403) Forbidden.
Source Error:
Line 40: _nmsm.AddNamespace("svg", "http://www.w3.org/2000/svg");
Line 41: _nmsm.AddNamespace("v", "http://schemas.microsoft.com/visio/2003/SVGExtensions/");
Line 42: xd.Load(xr);
Source File: path/to/the/file/file.cs Line: 42
Stack Trace:
[WebException: The remote server returned an error: (403) Forbidden.]
System.Net.HttpWebRequest.GetResponse() +5400333
System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials) +69
System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) +3929515
System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +54
System.Xml.XmlTextReaderImpl.OpenStream(Uri uri) +34
System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset(String systemId, String publicId) +380
[XmlException: An error has occurred while opening external DTD 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd': The remote server returned an error: (403) Forbidden.]
System.Xml.XmlTextReaderImpl.Throw(Exception e) +76
System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset(String systemId, String publicId) +513
System.Xml.DtdParserProxy.System.Xml.IDtdParserAdapter.PushExternalSubset(String systemId, String publicId) +16
System.Xml.DtdParser.ParseExternalSubset() +21
System.Xml.DtdParser.ParseInDocumentDtd(Boolean saveInternalSubset) +4017069
System.Xml.DtdParser.Parse(Boolean saveInternalSubset) +54
System.Xml.DtdParserProxy.Parse(Boolean saveInternalSubset) +31
System.Xml.XmlTextReaderImpl.ParseDoctypeDecl() +254
System.Xml.XmlTextReaderImpl.ParseDocumentContent() +451
System.Xml.XmlTextReaderImpl.Read() +151
System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) +58
System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) +20
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +129
System.Xml.XmlDocument.Load(XmlReader reader) +108
OurMethod(params) in path/to/the/file/file.cs:42
StackTrace in our App.
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3643; ASP.NET Version:2.0.50727.3634
起初我认为这是由于我们的 Internet 代理政策发生了变化,但由于在另一个代理后面的应用程序的另一个部署中发现了它,我非常怀疑这一点。
错误似乎或多或少是随机的,有时它会起作用,有时它不会。我只是不知道它为什么会这样。
问题是:你知道为什么会发生这种情况吗?如果是,您知道如何解决吗?如果没有,任何解决方法的想法?