有的时候我们需要在html显示xml,比如我们修改了xml,点击保存,需要在页面显示xml源码,让我们知道xml已经修改了,最好的方法是把xml放到pre元素中,但是会发现没有换行,全部显示一行,肯定很难看,所以我做了一个迭代xmlDOM的函数来格式显示xml的函数,
迭代函数思路:
1.每个xml文件时有无数个兄弟节点组成,但是终有最后截止的一个,那么循环结束的标志就是当一个节点没有兄弟节点时,循环就结束,
那么可以循环兄弟节点,于是有循环兄弟节点函数
2每个节点可能有子节点,子节点也可能有兄弟子节点,这个时候利用循环兄弟节点函数,循环节点的第一个子节点,
效果图:
主要代码:
private void getXMLStr(XmlDocument xmlDoc) { foreach (XmlNode node in xmlDoc.ChildNodes) { if (node.NodeType == XmlNodeType.Element) { getNext(node,0); } else { xml = "" + node.OuterXml.Replace("<","<").Replace(">",">"); } } } private void getNext(XmlNode node,int i) { if (node.NextSibling == null)//如果没有兄弟节点 { if (node.HasChildNodes) { //如果有子节点 if (node.FirstChild.NodeType != XmlNodeType.Text) { //getXmlAttribute(node) 获取节点的所有属性 //如果子节点的子节点不是text类型 xml = xml + "
<" + node.Name +" "+ getXmlAttribute(node) + ">
"; getNext(node.FirstChild, i + 1); xml = xml + ""+ node.Name +">
"; } else { //如果子节点的子节点不是text类型 xml = xml + "" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "
"; } } else { xml = xml + "" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "
"; } } else { if (node.HasChildNodes) { if (node.FirstChild.NodeType != XmlNodeType.Text) { xml = xml + "<" + node.Name+" " + getXmlAttribute(node) + ">
"; getNext(node.FirstChild, i + 1); xml = xml + ""+ node.Name + ">
"; } else { xml = xml + "" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "
"; } } else { xml = xml + "" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "
"; } getNext(node.NextSibling,i); } } private string getXmlAttribute(XmlNode node) { string rtn=string.Empty; foreach (XmlAttribute attr in node.Attributes) { rtn +=" "+ attr.Name + "=" + attr.Value; } return rtn; }
showXML.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="showXML.aspx.cs" Inherits="showXML" %>读取xml
showXML.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Xml; using System.Web.UI.HtmlControls; public partial class showXML : System.Web.UI.Page { public string xml = string.Empty; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } if (Request.QueryString["action"] != null && Request.QueryString["action"].ToString() != "") { switch (Request.QueryString["action"].ToString()) { case "create": Response.Clear(); Response.Write(showXml()); Response.End(); break; default: break; } } } ////// 在html显示xml /// ///文件名字private string showXml() { string rtn = string.Empty; string path = Server.MapPath("Xml\\") + "示例_创建" + ".xml"; //xml文件路径 if (File.Exists(path)) { XmlTextReader xmlRead = new XmlTextReader(path);//xml只读类 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlRead); xmlRead.Close(); getXMLStr(xmlDoc); rtn = xml; } return rtn; } private void getXMLStr(XmlDocument xmlDoc) { foreach (XmlNode node in xmlDoc.ChildNodes) { if (node.NodeType == XmlNodeType.Element) { getNext(node,0); } else { xml = "" + node.OuterXml.Replace("<","<").Replace(">",">"); } } } private void getNext(XmlNode node,int i) { if (node.NextSibling == null)//如果没有兄弟节点 { if (node.HasChildNodes) { //如果有子节点 if (node.FirstChild.NodeType != XmlNodeType.Text) { //getXmlAttribute(node) 获取节点的所有属性 //如果子节点的子节点不是text类型 xml = xml + "
<" + node.Name +" "+ getXmlAttribute(node) + ">
"; getNext(node.FirstChild, i + 1); xml = xml + ""+ node.Name +">
"; } else { //如果子节点的子节点不是text类型 xml = xml + "" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "
"; } } else { xml = xml + "" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "
"; } } else { if (node.HasChildNodes) { if (node.FirstChild.NodeType != XmlNodeType.Text) { xml = xml + "<" + node.Name+" " + getXmlAttribute(node) + ">
"; getNext(node.FirstChild, i + 1); xml = xml + ""+ node.Name + ">
"; } else { xml = xml + "" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "
"; } } else { xml = xml + "" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "
"; } getNext(node.NextSibling,i); } } private string getXmlAttribute(XmlNode node) { string rtn=string.Empty; foreach (XmlAttribute attr in node.Attributes) { rtn +=" "+ attr.Name + "=" + attr.Value; } return rtn; } }
示例_创建.xml源码
注意:xml路径与后天获取的xml的路径要一致,我的路径是程序根目录xml文件夹下
示例_创建.xml源码
11 12 13 14 15 16 21 22 23 24 25 26
以上就是xml学习(3) html显示xml的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!