new XElement("person", new XElement("name", "Gaby"), new XElement("age",18), new XElement("address","GuangZhou")),
new XElement("person", new XElement("name", "Lily"), new XElement("age",20), new XElement("address","USA")),
new XElement("person", new XElement("name", "Bill"), new XElement("age",55), new XElement("address","English"))
);
foreach (var item in xmlTree.Elements())
{
Console.WriteLine(item);
}
输出结果:
using System.Collections.Generic;
public partial class OperateXML:System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ///CreateXmlFile(); ///AddXmlElement(); ///UpdateXmlElement(); ///RemoveXmlElement(); ConvertXmlAttributeToElement(); }
private void CreateXmlFile()//创建XML文件 { ///设置新的XML文件保存的地址 string xmlFilePath = Server.MapPath("Data/new.xml"); XDocument doc = new XDocument( new XDeclaration("1.0","utf-8","yes"), new XElement("Books", new XElement("Book", new XAttribute("ID","104"), ///添加属性ID new XElement("No","0004"), ///添加元素No new XElement("Name","Book 0004"), ///添加元素Name new XElement("Price","300"), ///添加元素Price new XElement("Remark","This is a book 0004.") ///添加元素Remark ) ) ); ///保存为XML文件 doc.Save(xmlFilePath); ///显示XML文件的内容 Response.Write(doc); ///设置网页显示的形式为XML文件 Response.ContentType = "text/xml"; Response.End(); }
private void AddXmlElement()//增加元素到XML文件 { ///<Book ID="101"> /// <No>00001</No> /// <Name>Book 0001</Name> /// <Price>100</Price> /// <Remark>This is a book 00001.</Remark> ///</Book> ///导入XML文件 string xmlFilePath = Server.MapPath("Data/Books.xml"); XElement xe = XElement.Load(xmlFilePath); ///创建一个新的节点 XElement book = new XElement("Book", new XAttribute("ID","104"), ///添加属性ID new XElement("No","0004"), ///添加元素No new XElement("Name","Book 0004"), ///添加元素Name new XElement("Price","300"), ///添加元素Price new XElement("Remark","This is a book 0004.") ///添加元素Remark ); ///添加节点到文件中,并保存 xe.Add(book); xe.Save(xmlFilePath); ///显示XML文件的内容 Response.Write(xe); ///设置网页显示的形式为XML文件 Response.ContentType = "text/xml"; Response.End(); }
private void UpdateXmlElement()//修改XML文件中的元素 { ///导入XML文件 string xmlFilePath = Server.MapPath("Data/Books.xml"); XElement xe = XElement.Load(xmlFilePath); ///查找被替换的元素 IEnumerable<XElement> element = from e in xe.Elements("Book") where e.Attribute("ID").Value == "104" select e; ///替换为新元素,并保存 if (element.Count() > 0) { XElement first = element.First(); ///设置新的属性 first.SetAttributeValue("ID","106"); ///替换新的节点 first.ReplaceNodes( new XElement("No","0006"), ///添加元素No new XElement("Name","Book 0006"), ///添加元素Name new XElement("Price","600"), ///添加元素Price new XElement("Remark","This is a book 0006.") ///添加元素Remark ); } xe.Save(xmlFilePath); ///显示XML文件的内容 Response.Write(xe); ///设置网页显示的形式为XML文件 Response.ContentType = "text/xml"; Response.End(); }
private void RemoveXmlElement()//删除XML文件中的元素 { ///导入XML文件 string xmlFilePath = Server.MapPath("Data/Books.xml"); XElement xe = XElement.Load(xmlFilePath); ///查找被删除的元素 IEnumerable<XElement> element = from e in xe.Elements() where (string)e.Element("Name") == "Book 0003" select e; ///删除指定的元素,并保存 if(element.Count() > 0) { element.First().Remove(); } xe.Save(xmlFilePath); ///显示XML文件的内容 Response.Write(xe); ///设置网页显示的形式为XML文件 Response.ContentType = "text/xml"; Response.End(); }
private void ConvertXmlAttributeToElement()//将XML文件中的属性转换为元素 { ///导入XML文件 string xmlFilePath = Server.MapPath("Data/Books.xml"); XElement xe = XElement.Load(xmlFilePath); ///查找被替换的元素 IEnumerable<XElement> element = from e in xe.Elements("Book") where e.Attribute("ID").Value == "106" select e; ///替换为新元素,并保存 if (element.Count() > 0) { XElement first = element.First(); ///获取第一个属性 XAttribute attribute = first.FirstAttribute; ///将属性转换为元素 first.AddFirst( new XElement(attribute.Name,attribute.Value) ///添加元素ID ); ///删除属性 first.RemoveAttributes(); } xe.Save(xmlFilePath); ///显示XML文件的内容 Response.Write(xe); ///设置网页显示的形式为XML文件 Response.ContentType = "text/xml"; Response.End(); }
}
book.xml文件:
<?xml version="1.0" encoding="utf-8"?> <Books> <Book ID="101"> <No>00001</No> <Name>Book 0001</Name> <Price>100</Price> <Remark>This is a book 00001.</Remark> </Book> <Book ID="102"> <No>00002</No> <Name>Book 0002</Name> <Price>200</Price> <Remark>This is a book 00002.</Remark> </Book> <Book> <ID>106</ID> <No>0006</No> <Name>Book 0006</Name> <Price>600</Price> <Remark>This is a book 0006.</Remark> </Book> </Books>