“'html'元素未被声明。”在XmlValidatingReader中

时间:2011-12-13 15:51:38

标签: c# wcf doctype xmlreader

我有这个HTML文档:

    <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form class="Form" onsubmit="return checkForm(this);" id="Form" method="post">
//form body
</form>
</body>
</html>

这是堆栈跟踪:

  

at System.Xml.XmlValidatingReaderImpl.ValidationEventHandling.System.Xml.IValidationEventHandling.SendEvent(异常异常,XmlSeverityType严重性)      at System.Xml.Schema.BaseValidator.SendValidationEvent(String code,String arg)      在System.Xml.Schema.DtdValidator.ProcessElement()      在System.Xml.Schema.DtdValidator.Validate()      在System.Xml.XmlValidatingReaderImpl.Read()      在System.Xml.XmlReader.MoveToContent()      at System.ServiceModel.Channels.Message.CreateMessage(MessageVersion version,String action,XmlDictionaryReader body)      在Renault.LMT.ServiceModel.Dispatcher.ServerMessageFormatter.SerializeReply(MessageVersion messageVersion,Object []参数,对象结果)

代码,错误发生在最后一行:

MemoryStream MemoryStreamm = new MemoryStream(Encoding.UTF8.GetBytes((MessageBody)));
MemoryStreamm.Position = 0;
XmlReaderSettings settingsReader = new XmlReaderSettings();
settingsReader.DtdProcessing = DtdProcessing.Parse;
settingsReader.ValidationType = ValidationType.DTD;
settingsReader.XmlResolver = null;
XmlReader reader = XmlReader.Create(MemoryStreamm, settingsReader);

MessageResponse = Message.CreateMessage(messageVersion, string.Format("ServiceModel/ILMTService/{0}", Operation), reader);

1 个答案:

答案 0 :(得分:1)

根据http://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings.xmlresolver.aspx,将XmlResolver设置为null似乎不是一个好主意。可能无法加载DTD,因此无法匹配任何元素,第一个元素是html

我强烈建议您在本地存储DTD的副本,并实现XmtResolver,当请求DTD时,返回该本地副本。您应该始终为DTD和XML架构执行此操作,因为提供这些文件的许多服务器会严重限制来自任何一个位置的请求数。