c# 读取XML文件的简单代码
本文导语: 1、xml文件 代码示例: Neoplasms Cancers 2、读取xml文件的代码 代码示例: /// /// 读XML文件 /// /// /// using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.We...
1、xml文件
Neoplasms
Cancers
2、读取xml文件的代码
///
/// 读XML文件
///
///
///
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
ReaderXML(@Server.MapPath("XMLFile.xml"));
}
private void ReaderXML(string strPath)
{
string Msg="";
XmlTextReader xtr = new XmlTextReader(strPath);
while(xtr.Read())
{
switch(xtr.NodeType)
{
case XmlNodeType.Element:
{
Response.Write(Msg += string.Format("开始元素{0}n", xtr.Name) + "
");
//是否有属性
if(xtr.HasAttributes)
{
//读属性
for(int i = 0;i < xtr.AttributeCount;i++)
{
xtr.MoveToAttribute(i);
Response.Write(Msg += string.Format("属性{0} = '{1}'n", xtr.Name, xtr.Value) + "
");
}
}
break;
}
case XmlNodeType.Text:
{
Response.Write(Msg += string.Format("内容{0}n", xtr.Value) + "
");
break;
}
case XmlNodeType.EndElement:
{
Response.Write(Msg += string.Format("结束元素{0}n", xtr.Name) + "
");
break;
}
}
}
xtr.Close();
}
}