62,046
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="big5" ?>
- <LINEUP>
<TEAM_INFO HOME_ID="4" GAME_TIME="2009/04/13 19:15"/>
- <PLAYER_LIST>
<PLAYER PLAYER_NAME="C罗" PLAYER_NUMBER="12" POSITION="前锋" STATUS_ID="0" />
<PLAYER PLAYER_NAME="贝壳汉姆" POSITION="中场" STATUS_ID="0" />
<PLAYER PLAYER_NAME="李玮峰" PLAYER_NUMBER="25" POSITION="门" STATUS_ID="0" />
</PLAYER_LIST>
</LINEUP>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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)
{
XmlDocument xd = new XmlDocument();
xd.Load(Request.PhysicalApplicationPath + "Books.xml");
XmlNodeList xnl = xd.GetElementsByTagName("PLAYER");
foreach (XmlElement xe in xnl)
{
if (xe.Attributes["PLAYER_NUMBER"] != null)
{
Response.Write(xe.Attributes["PLAYER_NUMBER"].Value + "<br/>");
}
else
{
XmlAttribute xa = xd.CreateAttribute(null, "PLAYER_NUMBER", null);
xa.Value = "0";
xe.Attributes.Append(xa);
}
}
xd.Save(Server.MapPath("Books.xml"));
//Response.Write(xd.OuterXml);
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.IO;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int bookcount = 0;
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
//我们不是创建XmlReader类的的具体实现代码,而是创建XmlReaderSettings类的一个实力
//,把它传给Create方法,用XmlReaderSettings类为XmlReader对象指定需要的供能
string booksFile = Path.Combine(Request.PhysicalApplicationPath, "Books.xml");
using (XmlReader reader = XmlReader.Create(booksFile, settings))
{
while (reader.Read())//如果成功读取了节点,就返回True,当节点都读取完了,它就返回false
{
if (reader.NodeType == XmlNodeType.Element && reader.HasAttributes)
{
if (reader["PLAYER_NUMBER"] != null)
{
Response.Write(reader["PLAYER_NUMBER"].ToString()+" ");
}
//Response.Write(reader.GetAttribute("PLAYER_NUMBER")+"<br/>");
//bookcount++;
}
}
}
//Response.Write(String.Format("Found {0} books!", bookcount));
}
}