2010-12-14 9 views
11

를 검색에서 나는 코드 (C#을) 다음과 같은 한, 그것은 너무 오래 걸립니다 그리고 그것은 예외가 발생합니다 : 나는 그 않는 이유를 이해.NET : 방지 XmlDocument.LoadXml DTD

new XmlDocument(). 
LoadXml("<?xml version='1.0' ?><!DOCTYPE note SYSTEM 'http://someserver/dtd'><note></note>"); 

합니다. 제 질문은 어떻게 멈추게할까요? 나는 DTD 유효성 검사에 신경 쓰지 않는다. 난 그냥 정규식을 대체 할 수있을 것 같아요,하지만 난 더 우아한 솔루션을 찾고 있습니다.

배경 :
실제 XML은 내가 소유하지 않은 웹 사이트에서 수신되었습니다. 사이트가 유지 보수 중일 때 유지 보수 중에 사용할 수없는 DTD를 가리키는 DOCTYPE을 사용하여 XML을 리턴합니다. 그래서 필자의 서비스는 불필요하게 느려지는데, XML을 파싱 할 때마다 DTD를 얻으려고하기 때문이다.

Unhandled Exception: System.Net.WebException: The remote name could not be resolved: 'someserver' 
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.XmlLoader.LoadDocSequence(XmlDocument parentDoc) 
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) 
at System.Xml.XmlDocument.Load(XmlReader reader) 
at System.Xml.XmlDocument.LoadXml(String xml) 
at ConsoleApplication36.Program.Main(String[] args) in c:\Projects\temp\ConsoleApplication36\Program.cs:line 11 

답변

10

글쎄, .NET 4.0 XmlTextReader는이 DtdProcessing라는 속성이 있습니다 : 여기

예외 스택이다. DtdProcessing.Ignore로 설정하면 DTD 처리가 비활성화됩니다.

+0

어떨까요. –

+0

XmlReader.Settings.ValidationType을 ValidationType.None으로 설정해야합니다. 또는 XmlReader.Settings.XmlResolver를 null로 설정하면 트릭을 수행 할 수 있다고 생각합니다. –

+13

확인 된'doc.XmlResolver = null; '은 문제를 해결합니다. –

0

.net 4.5.1. doc.XmlResolver를 null로 설정하는 행운이 없었습니다.

가장 쉬운 해결 방법은 문자열 바꾸기를 사용하여 LoadXml()을 호출하기 전에 "xmlns ="를 "ignore ="로 변경하는 것입니다.

var responseText = await response.Content.ReadAsStringAsync(); 
responseText = responseText.Replace("xmlns=", "ignore="); 
try 
{ 
    var doc = new XmlDocument(); 
    doc.LoadXml(responseText); 
    ... 
}