直接采用XmlReader.Create(fs);会报错误 缺少根节点 这是什么原因,应该怎么做请高手路过时看看
用其他的方式实现视图引擎,可能很难实现强类型的model,而采用XSLT作为视图引擎则可以实现
可以将实体对象序列化为XML,xml在解析为html这样就实现了强类型的MODEL
今天在试验时发现个问题,始终未能直接解决,
XmlReader.Create(fs);会报缺少根节点呢 fs为一个MemoryStream
代码如下
1强类型的MODEL
[Serializable] public class ChangePasswordModel { public string OldPassword { get; set; } public string NewPassword { get; set; } public string ConfirmPassword { get; set; } }2xsltView:IView
进行解析
void IView.Render(ViewContext viewContext, System.IO.TextWriter writer) { XslCompiledTransform transform = new XslCompiledTransform(); //xslt文件的路径 string XsltFileDir =System.Web.HttpContext.Current.Server.MapPath(_xsltPhysicalPath); try { //XsltSettings myxsltseting = new XsltSettings(); //myxsltseting.EnableDocumentFunction = true; transform.Load(XsltFileDir); } catch (Exception ex) { throw ex; } MemoryStream stream = new MemoryStream(); MemoryStream fs = new MemoryStream(); XmlReaderSettings redset = new XmlReaderSettings(); redset.ProhibitDtd = true; XsltArgumentList myarg = new XsltArgumentList(); myarg.AddExtensionObject("myfn:utils", new MyXslExtension()); System.Xml.Serialization.XmlSerializer xmlser = new System.Xml.Serialization.XmlSerializer(viewContext.ViewData.Model.GetType()); xmlser.Serialize(fs, viewContext.ViewData.Model); fs.Position = 0; StreamReader reader2 = new StreamReader(fs, System.Text.Encoding.UTF8); if (string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Params["debug"])) { try { //原来采用ViewData存数据的时候 // transform.Transform(XmlReader.Create(new StringReader(viewContext.ViewData["xmlcontent"].ToString()), redset),myarg, stream); /// ///XmlReader.Create(fs);会报缺少根节点呢 transform.Transform(XmlReader.Create(new StringReader(reader2.ReadToEnd()),redset), myarg, stream); } catch (Exception ex) { throw ex; } // transform.Transform(Server.MapPath("a.xml"), null, stream); stream.Position = 0; StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8); // 呈现出解析后的内容 writer.Write(reader.ReadToEnd()); } else { writer.Write(reader2.ReadToEnd()); } }
如果直接采用XmlReader.Create(fs);会报错误 缺少根节点 这是什么原因,应该怎么做请高手路过时看看
test