Xml数据源为:
admin 127.0.0.1 admin登录系统 admin 127.0.0.1 admin删除了课件管理中id为11数据 admin 127.0.0.1 admin在课件分类管理中id为0下添加技术类分类 admin 127.0.0.1 admin在课件中课件分类id为48中添加技术一课件 admin 127.0.0.1 admin登录系统 admin 127.0.0.1 admin在课件中课件分类id为48中添加技术课程课件
查询节点UserName=admin的Action的节点值
////// linq 查询Xml /// private static void linqToXml() { string file = File.ReadAllText("2012-03-21.xml", Encoding.Default); XElement element = XElement.Parse(file); var items = from book in element.Elements("Data") where (string)book.Element("username") == "admin" select book.Element("action"); foreach (var item in items) { Console.WriteLine(item.Value); } Console.ReadKey(); }
查询结果为:
admin登录系统
admin删除了课件管理中id为11数据admin在课件分类管理中id为0下添加技术类分类admin在课件中课件分类id为48中添加技术一课件admin登录系统admin在课件中课件分类id为48中添加技术课程课件=====================================================================================
Xml操作
一:创建XML
private static void linqToCreateXML() { XElement elment = new XElement("Data", new XElement("aaa", new XElement("name", "lily"), new XElement("age", "10")), new XElement("aaa", new XElement("name", "jak"), new XElement("age", "110"))); Console.WriteLine(elment.ToString()); Console.WriteLine("========================"); //创建Xml XDocument doc = new XDocument(new XElement("Data", new XElement("aaa", new XElement("name", "lily"), new XElement("age", "10")), new XElement("aaa", new XElement("name", "jak"), new XElement("age", "110")))); //为Xml之追加节点,并且设置属性 doc.Element("Data").Add( new XElement("name", new XAttribute("type", "11"), "asasas"), new XElement("age", "222")); Console.WriteLine(doc.ToString()); Console.ReadLine(); }
运行结果:
lily 10 ======================== jak 110 lily 10 jak 110 asasas 222
==================================================================================
修改Xml指定节点的属性值
////// 修改Xml属性值 /// private static void LinqToUpdateAttrbut() { //创建Xml XDocument doc = new XDocument(new XElement("Data", new XElement("aaa", new XElement("name", "lily"), new XElement("age", "10")), new XElement("aaa", new XElement("name", "jak"), new XElement("age", "110")))); //为Xml之追加节点,并且设置属性 doc.Element("Data").Add( new XElement("name", new XAttribute("type", "11"), "asasas"), new XElement("age", "222")); Console.WriteLine(doc.ToString()); Console.WriteLine("===========2============="); //修改节点Name的属性,当属性值为11的时候,则修改为“我是修改后的” foreach (XAttribute attr in doc.Element("Data").Element("name").Attributes("type")) { if (attr.Value == "11") { attr.Value = "我是修改后的"; } } Console.WriteLine(doc.ToString()); Console.WriteLine("=============2==========="); Console.ReadLine(); }
运行结果:
lily 10 jak 110 asasas 222 ===========2=============lily 10 jak 110 asasas 222 =============2===========
=====================================================================================
修改节点值
将name节点值为lily的age字段值修改为0(将lily的年龄修改为0岁)
private static void LinqToUpdateValue() { //创建Xml XDocument doc = new XDocument(new XElement("Data", new XElement("aaa", new XElement("name", "lily"), new XElement("age", "10")), new XElement("aaa", new XElement("name", "jak"), new XElement("age", "110")))); //为Xml之追加节点,并且设置属性 doc.Element("Data").Add( new XElement("name", new XAttribute("type", "11"), "asasas"), new XElement("age", "222")); Console.WriteLine(doc.ToString()); //查询年龄为lily的节点 var s1 = from s in doc.Element("Data").Elements("aaa") where s.Element("name").Value == "lily" select s; Console.WriteLine("===========2============="); //修改年龄节点为0 foreach (var attr in s1) { attr.Element("age").SetValue("0"); } Console.WriteLine(doc.ToString()); Console.WriteLine("=============2==========="); Console.ReadLine(); }
运行结果:
lily 10 jak 110 asasas 222 ===========2=============lily 0 jak 110 asasas 222 =============2===========
=========================================================================
为某节点设置属性1
SetAttributeValue(属性名, 属性值);
:当此节点没有属性的时候,则添加属性,当节点已经存在此数字的时候则修改属性
private static void LinqToUpdateAttrbut1() { //创建Xml XDocument doc = new XDocument(new XElement("Data", new XElement("aaa", new XElement("name", "lily"), new XElement("age", "10")), new XElement("aaa", new XElement("name", "jak"), new XElement("age", "110")))); //为Xml之追加节点,并且设置属性 doc.Element("Data").Add( new XElement("name", new XElement("age", "222"))); Console.WriteLine(doc.ToString()); Console.WriteLine("===========添加了属性============"); //对指定节点添加属性 doc.Element("Data").Element("aaa").Element("name").SetAttributeValue("aaaa", "ssssssss"); Console.WriteLine(doc.ToString()); //// //文档更新触发事件 doc.Changed += new EventHandler(doc_Changed); // //Console.WriteLine(doc.ToString()); Console.WriteLine("===========我修改了属性属性============"); //对指定节点更新属性 doc.Element("Data").Element("aaa").Element("name").SetAttributeValue("aaaa", "ccccccc"); Console.WriteLine(doc.ToString()); Console.ReadLine(); } static void doc_Changed(object sender, XObjectChangeEventArgs e) { Console.WriteLine("我是文档修改的事件,我抓住了,"); }
运行结果为:
lily 10 jak 110 ===========添加了属性============ 222 lily 10 jak 110 ===========我修改了属性属性============我是文档修改的事件,我抓住了, 222 lily 10 jak 110 222