public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { XmlDocument document = new XmlDocument(); AddElement(document); RepalceElement(document); RepalceAttrbute(document); InsertElement(document); RemoveElement(document); }
private void AddElement(XmlDocument document) //添加元素 { XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "gb2312", "yes"); document.AppendChild(declaration); XmlElement bookselement = document.CreateElement("books"); document.AppendChild(bookselement); XmlElement bkelement = document.CreateElement("book"); bookselement.AppendChild(bkelement); XmlElement nameelement = document.CreateElement("name"); nameelement.InnerText = "java"; bkelement.AppendChild(nameelement); XmlElement priceelement = document.CreateElement("price"); priceelement.InnerText = "120"; bkelement.AppendChild(priceelement); XmlElement authorelement = document.CreateElement("author"); authorelement.SetAttribute("id", "1"); authorelement.InnerText = "welinmus"; bkelement.AppendChild(authorelement);
bkelement = document.CreateElement("book"); bookselement.AppendChild(bkelement); nameelement = document.CreateElement("name"); nameelement.InnerText = "XML"; bkelement.AppendChild(nameelement); priceelement = document.CreateElement("price"); priceelement.InnerText = "100"; bkelement.AppendChild(priceelement); authorelement = document.CreateElement("author"); authorelement.SetAttribute("id", "2"); authorelement.InnerText = "Lobinshion"; bkelement.AppendChild(authorelement);
bkelement = document.CreateElement("book"); bookselement.AppendChild(bkelement); nameelement = document.CreateElement("name"); nameelement.InnerText = "SQLServer"; bkelement.AppendChild(nameelement); priceelement = document.CreateElement("price"); priceelement.InnerText = "110"; bkelement.AppendChild(priceelement); authorelement = document.CreateElement("author"); authorelement.SetAttribute("id", "3"); authorelement.InnerText = "John.Smith"; bkelement.AppendChild(authorelement);
document.Save(Server.MapPath("books.xml")); }
private void RepalceElement(XmlDocument document) //替换文本节点 { document.Load(Server.MapPath("books.xml")); XmlNodeList nodelist=document.GetElementsByTagName("*"); XmlElement element; for (int i = 0; i < nodelist.Count; i++) { element =(XmlElement) nodelist[i];
if (element.Name == "name") { if (element.ChildNodes[0].Value == "java") { element.InnerText ="jsp"; break; } } }
document.Save(Server.MapPath("books.xml")); }
private void RepalceAttrbute(XmlDocument document) //替换属性节点 { document.Load(Server.MapPath("books.xml")); XmlNodeList nodelist = document.GetElementsByTagName("*"); XmlElement element;
for (int i = 0; i < nodelist.Count; i++) { element = (XmlElement)nodelist[i];
if (element.GetAttribute("id") == "1") { element.SetAttribute("id", "5"); break; } }
document.Save(Server.MapPath("books.xml")); }
private void InsertElement(XmlDocument document) //插入元素 { document.Load(Server.MapPath("books.xml")); XmlNodeList nodelist = document.GetElementsByTagName("*"); XmlElement element; XmlElement pageelement=document.CreateElement("page"); pageelement.InnerText="350页";
for (int i = 0; i < nodelist.Count; i++) { element = (XmlElement)nodelist[i];
if (element.Name == "book") { if (element.ChildNodes[0].InnerText=="XML") { element.InsertAfter(pageelement, element.FirstChild); break; } } } document.Save(Server.MapPath("books.xml")); }
private void RemoveElement(XmlDocument document) //删除元素 { document.Load(Server.MapPath("books.xml")); XmlNodeList nodelist = document.GetElementsByTagName("*"); XmlElement element; for (int i = 0; i < nodelist.Count; i++) { element = (XmlElement)nodelist[i]; if (element.Name=="book") { if (element.ChildNodes[0].InnerText=="XML") element.RemoveChild(element.ChildNodes[1]); } }
document.Save(Server.MapPath("books.xml")); } }