2011-01-27 34 views
1

我正在構建一個自定義XML文檔。在XmlDocument.CreateDocumentType方法中超時

以下代碼需要30秒才能執行(請參閱註釋)爲什麼?

var doc = new XmlDocument(); 
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null)); 

// hangs on next line - doc.CreateDocumentType() 
var xmlDocType = doc.CreateDocumentType(
      "svg" 
      , "-//W3C//DTD SVG 20001102//EN" 
      , "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd" 
      , null); 

doc.AppendChild(xmlDocType); 

當這發生在30秒內,我得到以下異常:

System.Net.WebException:

"The underlying connection was closed: The connection was closed unexpectedly." 

Exception Stack Trace:

at System.Net.HttpWebRequest.GetResponse() 
at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy) 
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy) 
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) 
at System.Xml.XmlTextReaderImpl.OpenAndPush(Uri uri) 
at System.Xml.XmlTextReaderImpl.PushExternalEntityOrSubset(String publicId, String systemId, String 

回答

5

可能是因爲您的解析器正試圖從www.w3.org獲取指定的DTD。

由於交通繁忙,他們不再直接投放DTD(詳情請參閱this link)。

您需要從他們的W3C網站下載DTD,將它們緩存在本地,並讓您的解析器將指定的URL映射到本地緩存副本。