LINQ to XML操作XML文件的方法,如创建XML文件、添加新的元素到XML文件中、修改XML文件中的元素、删除XML文件中的元素等。
1. 创建XML文件
string xmlFilePath = Server.MapPath("Data/Product.xml");XDocument doc = new XDocument (new XDeclaration("1.0", "utf-8", "yes"),new XElement("Products",new XElement("Product",new XAttribute("ID", 1),new XElement("ProductName", "LINQ to XML"),new XElement("UnitPrice", 10),new XElement("Remark", "LINQ to XML操作"))) );// 保存为XML文件 doc.Save(xmlFilePath);// 显示XML文件内容 Response.Write(doc); // 设置网页显示的形式为XML文件 Response.ContentType = "text/xml"; Response.End();
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <Products><Product ID="1"><ProductName>LINQ to XML</ProductName><UnitPrice>10</UnitPrice><Remark>LINQ to XML操作</Remark></Product> </Products>
XElement xml = new XElement ("Root",new XElement("Product",new XAttribute("ID", 1)) );xml.Save(Environment.CurrentDirectory + @"\" + "Product.xml");
<?xml version="1.0" encoding="utf-8"?> <Root><Product ID="1" /> </Root>
根据从数据库中查询的数据结构生成一个XML文件:
using (NorthwindContext context = new NorthwindContext()) {var products = from p in context.Productsselect new{p.ProductID,p.ProductName,p.UnitPrice};var xml = new XElement("Products",from p in products.ToList()select new XElement("Product",new XElement("ProductID", p.ProductID),new XElement("ProductName", p.ProductName),new XElement("UnitPrice", p.UnitPrice)));xml.Save(Environment.CurrentDirectory + @"\" + "Products.xml"); }
2. 加载XML文件
XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\Product.xml");foreach (XNode node in doc.Nodes()) {Console.WriteLine(node.ToString()); }
运行输出:
<!--Created by LINQ to XML--> <Products><Product><ProductID>1</ProductID><ProductName>LINQ to XML</ProductName><UnitPrice>10</UnitPrice></Product><Product><ProductID>2</ProductID><ProductName>LINQ to SQL</ProductName><UnitPrice>20</UnitPrice></Product> </Products>
XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\Product.xml");foreach (XNode node in doc.Nodes().OfType<XElement>()) {Console.WriteLine(node.ToString()); }
<Products><Product><ProductID>1</ProductID><ProductName>LINQ to XML</ProductName><UnitPrice>10</UnitPrice></Product><Product><ProductID>2</ProductID><ProductName>LINQ to SQL</ProductName><UnitPrice>20</UnitPrice></Product> </Products>
XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\Product.xml");foreach (XElement element in doc.Elements()) {Console.WriteLine(element.ToString()); }
XElement el = XElement.Load(Environment.CurrentDirectory + @"\Product.xml");foreach (XElement e in el.Elements("Product")) {Console.WriteLine(e.Element("ProductName").Value); }
XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\" + "Product.xml");var query = from p in doc.Descendants("Product")where p.Attribute("ID").Value == "1"select p;foreach (var item in query) {Console.WriteLine(item.Element("ProductName").Value); }
XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\" + "Product.xml");var query = from p in doc.Root.Elements("Product")select new{ProductID = p.Element("ProductID").Value,ProductName = p.Element("ProductName").Value,UnitPrice = p.Element("UnitPrice").Value}; foreach (var item in query) {Console.WriteLine("{0}-{1}-{2}", item.ProductID, item.ProductName, item.UnitPrice); }
3. 添加元素到XML文件
XElement el = XElement.Load(Server.MapPath("Data/Product.xml"));XElement product = new XElement ("Product",new XAttribute("ID", 2),new XElement("ProductName", "LINQ to Object"),new XElement("UnitPrice", 20m),new XElement("Remark", "") );el.Add(product); el.Save(Server.MapPath("Data/Product.xml"));
<?xml version="1.0" encoding="utf-8"?> <Products><Product ID="1"><ProductName>LINQ to XML</ProductName><UnitPrice>10</UnitPrice><Remark>LINQ to XML操作</Remark></Product><Product ID="2"><ProductName>LINQ to Object</ProductName><UnitPrice>20</UnitPrice><Remark></Remark></Product> </Products>
4. 修改XML文件中元素
XElement el = XElement.Load(Server.MapPath("Data/Product.xml"));IEnumerable<XElement> products = from e in el.Elements("Product")where e.Attribute("ID").Value == "1"select e; if (products.Count() > 0) {XElement product = products.First();product.SetAttributeValue("ID", 3);product.ReplaceNodes(new XElement("ProductName", "LINQ to XML Version 2"),new XElement("UnitPrice", 30)); }el.Save(Server.MapPath("Data/Product.xml"));
<?xml version="1.0" encoding="utf-8"?> <Products><Product ID="3"><ProductName>LINQ to XML Version 2</ProductName><UnitPrice>30</UnitPrice></Product><Product ID="2"><ProductName>LINQ to Object</ProductName><UnitPrice>20</UnitPrice><Remark></Remark></Product> </Products>
5. 删除XML文件中的元素
XElement el = XElement.Load(Server.MapPath("Data/Product.xml"));IEnumerable<XElement> products = from e in el.Elements("Product")where e.Attribute("ID").Value == "2"select e; if (products.Count() > 0) {products.First().Remove(); }el.Save(Server.MapPath("Data/Product.xml"));
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml"); xml.Element("Product").Remove(); // 删除第一个Product子元素 xml.Elements("Product").Remove(); // 删除全部Product子元素 xml.Save(Environment.CurrentDirectory + @"\" + "Product.xml");
xml.SetElementValue("Product", null); // 删除第一个Product子元素
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml"); xml.Element("Product").SetElementValue("ProductID", 1); // 修改ProductID子元素 xml.Element("Product").SetElementValue("ProductID", null); // 删除ProductID子元素
6. 将XML文件中属性转换为元素
XElement el = XElement.Load(Server.MapPath("Data/Product.xml"));IEnumerable<XElement> products = from e in el.Elements("Product")where e.Attribute("ID").Value == "1"select e; if (products.Count() > 0) {XAttribute attr = products.First().FirstAttribute;products.First().AddFirst(new XElement(attr.Name, attr.Value));attr.Remove(); }el.Save(Server.MapPath("Data/Product.xml"));
<?xml version="1.0" encoding="utf-8"?> <Products><Product><ID>1</ID><ProductName>LINQ to XML</ProductName><UnitPrice>30</UnitPrice></Product> </Products>
7.XML节点属性
1> 添加属性
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml");XElement product=(from p in xml.Elements("Product")select p).First(); product.Add(new XAttribute("ID", 1));xml.Save(Environment.CurrentDirectory + @"\" + "Product.xml");
2> 修改属性
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml");XElement product = (from p in xml.Elements("Product")select p).First(); product.SetAttributeValue("ID", 2);xml.Save(Environment.CurrentDirectory + @"\" + "Product.xml");
3> 删除属性
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml");XElement product = (from p in xml.Elements("Product")select p).First(); product.Attribute("ID").Remove();xml.Save(Environment.CurrentDirectory + @"\" + "Product.xml");