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();
LINQ to XML 10 LINQ to XML操作
XElement xml = new XElement( "Root", new XElement("Product", new XAttribute("ID", 1) ));xml.Save(Environment.CurrentDirectory + @"\" + "Product.xml");
根据从数据库中查询的数据结构生成一个XML文件:
using (NorthwindContext context = new NorthwindContext()){ var products = from p in context.Products select 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());}
运行输出:
1 LINQ to XML 10 2 LINQ to SQL 20
XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\Product.xml");foreach (XNode node in doc.Nodes().OfType()){ Console.WriteLine(node.ToString());}
1 LINQ to XML 10 2 LINQ to SQL 20
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"));
LINQ to XML 10 LINQ to XML操作 LINQ to Object 20
4. 修改XML文件中元素
XElement el = XElement.Load(Server.MapPath("Data/Product.xml"));IEnumerableproducts = 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"));
LINQ to XML Version 2 30 LINQ to Object 20
5. 删除XML文件中的元素
XElement el = XElement.Load(Server.MapPath("Data/Product.xml"));IEnumerableproducts = 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"));IEnumerableproducts = 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"));
1 LINQ to XML 30
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");