问题倒是不难
帮楼主写了一个,是采用递归的方式.
不过还是建议楼主在节点当中加上一个parent属性,或是深度属性,那样读取和解析起来更有效率.<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml"%>
<%@ Import Namespace="System.Xml.XPath"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">
private XmlDocument doc = new XmlDocument(); protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
doc.Load(Server.MapPath("City.xml"));
XmlElement root = doc.DocumentElement;
PasterXml(root,0);
}
}
protected void PasterXml(XmlElement root, int depth)
{
foreach (XmlElement childElem in root.ChildNodes)
{
if (depth == 0)
{
Response.Write(childElem.Attributes["name"].Value);
Response.Write("<br />");
}
else
{
Response.Write(childElem.Attributes["name"].Value + " " + childElem.ParentNode.Attributes["name"].Value);
Response.Write("<br />");
}
PasterXml(childElem, 1);
}
}</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>READ</title>
</head><body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
帮楼主写了一个,是采用递归的方式.
不过还是建议楼主在节点当中加上一个parent属性,或是深度属性,那样读取和解析起来更有效率.<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml"%>
<%@ Import Namespace="System.Xml.XPath"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">
private XmlDocument doc = new XmlDocument(); protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
doc.Load(Server.MapPath("City.xml"));
XmlElement root = doc.DocumentElement;
PasterXml(root,0);
}
}
protected void PasterXml(XmlElement root, int depth)
{
foreach (XmlElement childElem in root.ChildNodes)
{
if (depth == 0)
{
Response.Write(childElem.Attributes["name"].Value);
Response.Write("<br />");
}
else
{
Response.Write(childElem.Attributes["name"].Value + " " + childElem.ParentNode.Attributes["name"].Value);
Response.Write("<br />");
}
PasterXml(childElem, 1);
}
}</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>READ</title>
</head><body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
解决方案 »
- 开发已个信息采集系统?
- 如何设置Excel中的格式在页面才不会出现F1,F2
- vs2008开发网站链接数据库时总是提出错误:无法登录所请求的数据库
- 大家说做 sap开发有没前途,或者说.net是否有影响
- 急啊,ASP.NET+VB+Oracle采用<iewc:TreeView>建无限制级树,在线等待!!!
- 【散分】做了一个网站,给点建议和意见。
- 怎么实现这个(菜鸟问题)
- 新手请教:请问怎么把一个文本框里得到的字符类型的值转换成日期类型的值,在asp.net(vb.net)里。
- 一个简单、奇怪的问题--请看内容
- 这个怎么不是Group by表达式了?
- 奇怪而又简单的全文索引问题!!!
- 谁有好点的asp.net视频吗(不要基础的啊),谢谢
如果用的话 把路径改了就可以了
--------
你不测试怎么知道...那个0
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="MyLonol" %><script runat="server">
private XmlDocument doc = new XmlDocument(); protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
doc.Load(Server.MapPath("City.xml"));
XmlElement root = doc.DocumentElement;
PasterXml(root,0);
}
}
protected void PasterXml(XmlElement root, int depth)
{
foreach (XmlElement childElem in root.ChildNodes)
{
if (depth == 0)
{
Response.Write(childElem.Attributes["name"].Value);
Response.Write("<br />");
}
else
{
Response.Write(childElem.Attributes["name"].Value + " " + childElem.ParentNode.Attributes["name"].Value);
Response.Write("<br />");
}
PasterXml(childElem, 1);
}
}</script>……<table cellspacing="1" cellpadding="1" border="0" class="fleft" width="100%" style="line-height: 25px">
<tr align="center">
<th scope="col" style="font-size:12px;">
地区</th>
<th scope="col" style="font-size:12px;">
上级省市</th>
<th style="font-size:12px;">
管理栏目</th>
</tr>
<tr id="trData" runat="server" class="garybg" onmouseover="dieisinit.init(this);">
<td style="border-bottom:2px solid #E9FCFF; text-align:center; font-size:12px">
<%=Areas %>
</td>
<td style="border-bottom:2px solid #E9FCFF; text-align:center; font-size:12px">
<%=ParentArea %>
</td>
<td class="contorl" style="width: 115px; height: 24px; text-align: center; line-height: 24px;">
<a style="cursor: hand; text-decoration: underline; font-size:12px;" onclick='window.open("DelLog.aspx?id=<%=dt.Rows[i]["Addtime"].ToString()%>&adid=<%=Request["adid"] %>", "newwindow", "height=160, width=430, top=240, left=320, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no")'>
删除</a>
</td>
</tr>
</table>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="MyLonol" %> <script runat="server">
private XmlDocument doc = new XmlDocument(); protected string Areas, ParentArea; protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
doc.Load(Server.MapPath("City.xml"));
XmlElement root = doc.DocumentElement;
PasterXml(root,0);
}
}
protected void PasterXml(XmlElement root, int depth)
{
foreach (XmlElement childElem in root.ChildNodes)
{
if (depth == 0)
{
Areas=childElem.Attributes["name"].Value;
}
else
{
ParentArea = childElem.ParentNode.Attributes["name"].Value;
}
PasterXml(childElem, 1);
}
} </script> …… <table cellspacing="1" cellpadding="1" border="0" class="fleft" width="100%" style="line-height: 25px">
<tr align="center">
<th scope="col" style="font-size:12px;">
地区 </th>
<th scope="col" style="font-size:12px;">
上级省市 </th>
<th style="font-size:12px;">
管理栏目 </th>
</tr>
<tr id="trData" runat="server" class="garybg" onmouseover="dieisinit.init(this);">
<td style="border-bottom:2px solid #E9FCFF; text-align:center; font-size:12px">
<%=Areas %>
</td>
<td style="border-bottom:2px solid #E9FCFF; text-align:center; font-size:12px">
<%=ParentArea %>
</td>
<td class="contorl" style="width: 115px; height: 24px; text-align: center; line-height: 24px;">
<a style="cursor: hand; text-decoration: underline; font-size:12px;" onclick='window.open("DelLog.aspx?id= <%=dt.Rows[i]["Addtime"].ToString()%> &adid= <%=Request["adid"] %> ", "newwindow", "height=160, width=430, top=240, left=320, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no")'>
删除 </a>
</td>
</tr>
</table>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root code="root">
<list code="xinjiang" name="新疆">
<list code="aletai" name="阿勒泰">
<list code="wujiaqushi" name="五家渠市" />
<list code="tumusukeshi" name="图木舒克市" />
</list>
<list code="tacheng" name="塔城">
<list code="hebukesaiermenggu" name="和布克赛尔蒙古" />
<list code="yumingxian" name="裕民县" />
</list>
<list code="hetian" name="和田">
<list code="minfengxian" name="民丰县" />
<list code="yutianxian" name="于田县" />
</list>
</list>
<list code="ningxia" name="宁夏">
<list code="zhongwei" name="中卫">
<list code="haiyuanxian" name="海原县" />
<list code="zhongningxian" name="中宁县" />
</list>
<list code="guyuan" name="固原">
<list code="pengyangxian" name="彭阳县" />
<list code="jingyuanxian" name="泾源县" />
</list>
<list code="shizueshan" name="石嘴山">
<list code="pingluoxian" name="平罗县" />
<list code="huinongqu" name="惠农区" />
</list>
</list>
<root> 这个xml文件我如果想修改该怎么办呢?因为我这样修改的话:
XmlNodeList nodeList = xmlDoc.SelectSingleNode("root").ChildNodes;
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.GetAttribute("code") == Cid)
{
xe.SetAttribute("code", txtCode.Text);
xe.SetAttribute("name", txtName.Text);
break;
}
}
只能找到第一级结点,子节点和子节点的子节点确找不到!
请问该怎么办!?
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">
private XmlDocument doc = new XmlDocument(); protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
doc.Load(Server.MapPath("City.xml"));
XmlNodeList nodes = doc.SelectNodes("//list");
DList.DataSource = nodes;
DList.DataBind();
}
}
</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>READ</title>
</head><body>
<form id="form1" runat="server">
<div>
<asp:DataList runat="server" ID="DList">
<HeaderTemplate>
<table border="1"><tr><th>城市</th><th>父级城市</th><th>管理</th></tr>
</HeaderTemplate>
<ItemTemplate>
<tr><td><%#((XmlNode)Container.DataItem).Attributes["name"].Value%> </td>
<td><%#((XmlNode)Container.DataItem).ParentNode.Name == "root" ? "无" : ((XmlNode)Container.DataItem).ParentNode.Attributes["name"].Value%></td>
<td><asp:Button ID="btn1" runat="server" Text="manage" /></td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>