1个回答
2013-11-05
展开全部
C#的using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xml = new XmlDocument(); string path = Server.MapPath("~/test1.xml");
xml.Load(path);
//Response.Write(xml.DocumentElement.InnerText); //Response.Write("<br/>");
//Response.Write(xml.DocumentElement.InnerXml); //Response.Write(xml.DocumentElement.ChildNodes.Count);//所有的子节点 //for (int i = 0; i < xml.DocumentElement.ChildNodes.Count; i++)
//{
//Response.Write( ((XmlElement)xml.DocumentElement.ChildNodes[i]).GetAttribute("catalog").ToString());
// Response.Write(xml.DocumentElement.ChildNodes[i].OuterXml+"<br/>");
//Response.Write(xml.DocumentElement.ChildNodes[i].ParentNode.InnerText + "<br/>"); //添加节点
//XmlNode root = xml.DocumentElement;
//XmlElement elem = xml.CreateElement("id"); 创建节点
//elem.InnerText="1111";
//xml.DocumentElement.ChildNodes[i].AppendChild(elem);
//xml.DocumentElement.ChildNodes[i].RemoveChild(xml.DocumentElement.ChildNodes[i].FirstChild);
//xml.DocumentElement.ChildNodes[i].RemoveAll();
//xml.DocumentElement.RemoveAll(); //} //在根节点的末尾追加节点
//<book catalog="Chinese">
// <author>zhangsan</author>
// <year>2000</year>
// <price>15</price>
// <id>101010333</id>
//</book> //XmlElement elem1 = xml.CreateElement("author");
//elem1.InnerText = "zhangsan"; //XmlElement elem2 = xml.CreateElement("year");
//elem2.InnerText = "2000"; //XmlElement elem3 = xml.CreateElement("price");
//elem3.InnerText = "15"; //XmlElement elem4 = xml.CreateElement("id");
//elem4.InnerText = "101010333"; //XmlElement elem = xml.CreateElement("book");
//elem.SetAttribute("catalog", "Chinese"); //elem.AppendChild(elem1);
//elem.AppendChild(elem2);
//elem.AppendChild(elem3);
//elem.AppendChild(elem4);
//xml.DocumentElement.AppendChild(elem); //xml.Save(path);
//删除 year为1996的书节点 for (int i = 0; i < xml.DocumentElement.ChildNodes.Count; i++)
{
if (xml.DocumentElement.ChildNodes[i].ChildNodes[1].InnerText == "1996")
{
xml.DocumentElement.RemoveChild(xml.DocumentElement.ChildNodes[i]);
}
}
xml.Save(path);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xml = new XmlDocument(); string path = Server.MapPath("~/test1.xml");
xml.Load(path);
//Response.Write(xml.DocumentElement.InnerText); //Response.Write("<br/>");
//Response.Write(xml.DocumentElement.InnerXml); //Response.Write(xml.DocumentElement.ChildNodes.Count);//所有的子节点 //for (int i = 0; i < xml.DocumentElement.ChildNodes.Count; i++)
//{
//Response.Write( ((XmlElement)xml.DocumentElement.ChildNodes[i]).GetAttribute("catalog").ToString());
// Response.Write(xml.DocumentElement.ChildNodes[i].OuterXml+"<br/>");
//Response.Write(xml.DocumentElement.ChildNodes[i].ParentNode.InnerText + "<br/>"); //添加节点
//XmlNode root = xml.DocumentElement;
//XmlElement elem = xml.CreateElement("id"); 创建节点
//elem.InnerText="1111";
//xml.DocumentElement.ChildNodes[i].AppendChild(elem);
//xml.DocumentElement.ChildNodes[i].RemoveChild(xml.DocumentElement.ChildNodes[i].FirstChild);
//xml.DocumentElement.ChildNodes[i].RemoveAll();
//xml.DocumentElement.RemoveAll(); //} //在根节点的末尾追加节点
//<book catalog="Chinese">
// <author>zhangsan</author>
// <year>2000</year>
// <price>15</price>
// <id>101010333</id>
//</book> //XmlElement elem1 = xml.CreateElement("author");
//elem1.InnerText = "zhangsan"; //XmlElement elem2 = xml.CreateElement("year");
//elem2.InnerText = "2000"; //XmlElement elem3 = xml.CreateElement("price");
//elem3.InnerText = "15"; //XmlElement elem4 = xml.CreateElement("id");
//elem4.InnerText = "101010333"; //XmlElement elem = xml.CreateElement("book");
//elem.SetAttribute("catalog", "Chinese"); //elem.AppendChild(elem1);
//elem.AppendChild(elem2);
//elem.AppendChild(elem3);
//elem.AppendChild(elem4);
//xml.DocumentElement.AppendChild(elem); //xml.Save(path);
//删除 year为1996的书节点 for (int i = 0; i < xml.DocumentElement.ChildNodes.Count; i++)
{
if (xml.DocumentElement.ChildNodes[i].ChildNodes[1].InnerText == "1996")
{
xml.DocumentElement.RemoveChild(xml.DocumentElement.ChildNodes[i]);
}
}
xml.Save(path);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询