asp.net MVC 运用XML集合xsl在网页输出信息
2016-08-05 14:06
357 查看
Controller中记载Xml信息
Index.cshtml 页面展示
显示结果内容:
public ActionResult Index(string name) { if (name.Contains("Xml")) { ViewBag.Xml = ShowChange(); } return View(); } //改变格式内容赋值 public string ShowChange() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(AddNode()); XmlDocument xmlResult = new XmlDocument(); xmlResult.LoadXml( @"<?xml version=""1.0"" encoding=""utf-8""?> <xsl:stylesheet version =""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""> <xsl:template match=""/""> <div style=""padding-right: 0px; padding-left: 0px; padding-bottom: 6px; padding-top: 6px; text-align: center;"" > <table cellspacing=""0"" cellpadding=""0"" rules=""all"" border=""1"" id=""DataGridCoster"" style=""width:90%;border-collapse:collapse;table-layout: fixed; margin-top: 0px; ma 4000 rgin-bottom: 6px; font-size:12px;border: solid 1px #AACCEE;""> <tr style=""background-color:#AACCEE;height:30px; min-height:30px;"" > <td align=""center"" style=""width:40px;""> 序号 </td> <td align=""center"" style=""width:200px;"">变更项名称</td> <td align=""center"" style=""width:80px;"">变更项类别</td> <td align=""center"" >当前值</td> <td align=""center"" >变更为</td> <td align=""center"" style=""width:80px;"">是否生效</td> </tr> <xsl:for-each select=""/ChangeItems/ChangeItem""> <tr> <td align=""center""> <span> <xsl:value-of select=""position()"" /> </span> </td> <td align=""left""><xsl:value-of select=""@Name"" /></td> <td align=""center"" ><xsl:value-of select=""@ChangeType"" /></td> <td align=""center"" ><xsl:value-of select=""@OleValue"" disable-output-escaping=""yes"" /></td> <td align=""center"" ><xsl:value-of select=""@NewValue"" disable-output-escaping=""yes"" /></td> <td align=""center"" > <xsl:choose> <xsl:when test=""@SetFlag ='True'""> <img src='../images/accept.png' alt='已生效' title='已生效'/> </xsl:when> <xsl:otherwise> <img src='../images/que.gif' alt='未生效' title='未生效'/> </xsl:otherwise> </xsl:choose> </td> </tr> </xsl:for-each> </table> </div> </xsl:template> </xsl:stylesheet> "); StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); XslTransform transform = new XslTransform(); transform.Load(xmlResult); transform.Transform(xmlDoc, null, writer, null); return sb.ToString(); } //创建xml节点 public string AddNode() { XmlDocument xmlDoc = new XmlDocument(); XmlElement xelement = xmlDoc.CreateElement("ChangeItems"); xmlDoc.AppendChild(xelement); for (int i = 0; i < 2; i++) { XmlNode d = xmlDoc.CreateNode(XmlNodeType.Element, "ChangeItem", ""); XmlNode attr = xmlDoc.CreateNode(XmlNodeType.Attribute, "Name", ""); attr.Value = "凤舞九天"; d.Attributes.SetNamedItem(attr); attr = xmlDoc.CreateNode(XmlNodeType.Attribute, "Code", ""); attr.Value = "xz10086"; d.Attributes.SetNamedItem(attr); attr = xmlDoc.CreateNode(XmlNodeType.Attribute, "ChangeType", ""); attr.Value = "中文"; d.Attributes.SetNamedItem(attr); attr = xmlDoc.CreateNode(XmlNodeType.Attribute, "SetFlag", ""); d.Attributes.SetNamedItem(attr); attr = xmlDoc.CreateNode(XmlNodeType.Attribute, "OleValue", ""); attr.Value = "老纸"; d.Attributes.SetNamedItem(attr); attr = xmlDoc.CreateNode(XmlNodeType.Attribute, "NewValue", ""); attr.Value = "新知"; d.Attributes.SetNamedItem(attr); xelement.AppendChild(d); } return xmlDoc.InnerXml; }
Index.cshtml 页面展示
@Html.Raw(ViewBag.Xml)
显示结果内容:
相关文章推荐
- springmvc加入配置<aop:aspectj-autoproxy proxy-target-class="true"/>报错 org/aspectj/lang/annotation/Aroun
- ASP.NET Core开发-获取所有注入(DI)服务
- asp.net基础学习之前端页面布局
- Asp.Net使用代理IP远程获取数据
- AOP之AspectJ技术总汇
- asp.net实现中英文多域名检测的方法
- asp.net基础学习之控件的使用方法
- asp.net+xml+flash实现的图片展示效果示例
- 在 Linq 中使用 AsParallel 时自定义分块(Partitioners)
- 基于@Aspect的AOP
- aspectJ——helloworld
- asp.net实现导出DataTable数据到Word或者Excel的方法
- Spring A 4000 OP 的@Aspect
- VS2015 update3 安装 asp.net core 失败
- ASP.NET MVC5 新特性:Attribute路由使用详解
- ASP.NET中GridView、DataList、DataGrid三个数据控件foreach遍历用法示例
- asp.net基础学习之控件的使用方法
- asp.net基础学习之前端页面布局
- Mixing ASP.NET and MVC routing
- Java 8: 从永久代(PermGen)到元空间(Metaspace)