.NET: XML
2015-07-08 13:56
405 查看
XML在平常生活中用得很多,它的结构很简单,跟windows explorer有点像。
对它进行操作主要有三种方式:XmlDocument,
假设有这么一个XML文件Book.XML
View Code
对它进行操作主要有三种方式:XmlDocument,
假设有这么一个XML文件Book.XML
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace ConsoleTest { public class Program { static private void showXmlInfoByLinq(string path) { XElement xe = XElement.Load(path); IEnumerable<XElement> elements = from ele in xe.Elements("book") select ele; List<BookModel> modelList = new List<BookModel>(); foreach (var ele in elements) { BookModel model = new BookModel(); model.BookAuthor = ele.Element("author").Value; model.BookName = ele.Element("title").Value; model.BookPrice = Convert.ToDouble(ele.Element("price").Value); model.BookISBN = ele.Attribute("ISBN").Value; model.BookType = ele.Attribute("Type").Value; modelList.Add(model); } foreach (BookModel book in modelList) { Console.WriteLine("Book ISBN: {0} Type: {1}", book.BookISBN, book.BookType); Console.WriteLine("\tBookName: {0}", book.BookName); Console.WriteLine("\tBookAuthor: {0}", book.BookAuthor); Console.WriteLine("\tBookPrice: {0}", book.BookPrice); } } static void Main(string[] args) { const string PATH = @"C:\Users\Administrator\Desktop\Demo\Book.XML"; Console.WriteLine("\nRead by XmlLinq...\n"); showXmlInfoByLinq(PATH); XElement xe = XElement.Load(PATH); XElement record = new XElement( new XElement("book", new XAttribute("Type", "选修课"), new XAttribute("ISBN", "7-111-19149-8"), new XElement("title", "敏捷开发"), new XElement("author", "秦朗"), new XElement("price", 34.00) ) ); xe.Add(record); xe.Save(PATH); Console.WriteLine("\nApending one child by XmlLinq...\n"); showXmlInfoByLinq(PATH); xe = XElement.Load(PATH); IEnumerable<XElement> element = from ele in xe.Elements("book") where ele.Attribute("ISBN").Value == "7-111-19149-8" select ele; if (element.Count() > 0) { XElement first = element.First(); first.SetAttributeValue("Type", "必修课"); first.ReplaceNodes( new XElement("title", "敏捷开发框架"), new XElement("author", "秦明"), new XElement("price", 35.00) ); } xe.Save(PATH); Console.WriteLine("\nEditting one child by XmlLinq...\n"); showXmlInfoByLinq(PATH); xe = XElement.Load(PATH); IEnumerable<XElement> elements = from ele in xe.Elements("book") where ele.Attribute("ISBN").Value == "7-111-19149-8" select ele; if (elements.Count() > 0) { elements.First().Remove(); } xe.Save(PATH); Console.WriteLine("\nRemoving one child by XmlLinq...\n"); showXmlInfoByLinq(PATH); } } }
View Code
相关文章推荐
- 20世纪《福布斯》二十本最具影响力的商业书籍
- 如何给linux添加新硬盘
- [Selenium 2]—WebDriver 驱动说明
- maven 与 jenkins 集成的时候,打包出现错误处理
- [leedcode 24] Swap Nodes in Pairs
- opencv 绘图 cvLine cvRectangle cvCircle cvEllipse cvEllipseBox cvFillPoly cvConvexPoly cvPolyLine
- CSS 小技巧1——水平居中和垂直居中的方法
- searchbar下的搜索图标及textfiled样式修改
- editor多功能文本框在有些计算机上不能正常加载,解决方法,本人用的是把js调用方法放到<body></body>后面)
- const char与unsigned char的转换
- Lucas的数论(math)
- 讨价还价
- sizeof用法汇总
- STL - stack(栈)
- 每天一个linux命令(21):find命令之xargs
- STL - stack(栈)
- cookie 编程
- JavaScript权威指南_127_第15章_脚本化文档_15.5-元素的内容-Text节点
- StringUtils 工具类的常用方法
- 南阳oj 题目4 ASCII码排序