首页 > 代码库 > 解析xml
解析xml
cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace xmllianxi
{
public partial class WebForm1 : System.Web.UI.Page
{
Class1 info;
protected void Page_Load(object sender, EventArgs e)
{
List<Class1> list = new List<Class1>();
//实例化xml
XmlDocument xml = new XmlDocument();
//读取xml文件
xml.Load(@"i\XMLFile1.xml"); //你的xml地址
string id = "";
string name = "";
string sex = "";
//////////*******下面开始循环读取xml文件信息********/
///////////////
foreach (XmlNode node in xml.ChildNodes)
{
if (node.Name == "abc")
{
foreach (XmlNode node1 in node.ChildNodes)
{
if (node1.Name == "item")
{
foreach (XmlNode node2 in node1.ChildNodes)
{
switch (node2.Name)
{
case "id":
id = node2.InnerText;
break;
case "name":
name = node2.InnerText;
break;
default:
sex = node2.InnerText;
break;
}
}
info = new Class1(id, name, sex);
//将信息保存至集合
list.Add(info);
//Response.Write(list[0].Id + list[0].Name + list[0].Sex);
Response.Write(info.Name);
}
}
}
}
}
}
}
前台aspx代码
<body>
<form id="form1" runat="server">
<div>
<%=System.DateTime.Now.ToString()%>
<%=info.Name%>
</div>
</form>
</body>
解析xml