rotected void Page_Load(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("BookInfo.xml"));
XmlNode xn = xmlDoc.SelectSingleNode("books");//屏蔽第一行,取出根节点
XmlNodeList xnl = xn.ChildNodes;
StringBuilder s = new StringBuilder();
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
s.Append("属性:<br>"+xe.GetAttribute("genre")+"<br>");
s.Append("数据:<br><br>");
XmlNodeList xnll = xe.ChildNodes;
foreach (XmlNode f in xnll)
{
s.Append(f.InnerText+"<br>");
}
}
Response.Write(s);
}
插入节点
private void ShowDB()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("BookInfo.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load(Server.MapPath("BookInfo.xml"));
XmlNode root = xmlDoc.SelectSingleNode("books");//查找<books>
XmlElement xel = xmlDoc.CreateElement("book");//创建<book>节点
xel.SetAttribute("genre","计算机");
XmlElement xesub1 = xmlDoc.CreateElement("author");
xesub1.InnerText = "你大爷";
xel.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("price");
xesub2.InnerText = "150";
xel.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("bookname");
xesub3.InnerText="ASP.NET精通";
xel.AppendChild(xesub3);
root.AppendChild(xel);
xmlDoc.Save(Server.MapPath("BookInfo.xml"));
ShowDB();
}