135-1821-9792

LINQToXML怎么使用

这篇文章主要讲解了“LINQ To XML怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“LINQ To XML怎么使用”吧!

创新互联建站成都企业网站建设服务,提供网站设计制作、成都网站建设网站开发,网站定制,建网站,网站搭建,网站设计,成都响应式网站建设公司,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:18980820575

LINQ To XML——XML操作

XML数据越来越广泛地应用在各种实际的开发系统中,为了简化对XML数据的开发和利用,微软的开发团队开发了这个全新的LINQ to XML框架。首先,LINQ to XML框架是一个轻量级的XML编程API,开发者利用该框架,几乎可以取代原有的XML数据开发方式,非常简单地创建、读取并操作内存中的XML数据,如利用函数构造方法创建XML树等。其次,LINQ to XML框架中集成了LINQ的强大功能,开发者可以采用一致的编程方式,非常容易地操作并查询XML数据。

XElement Students = new XElement("Students",  new XElement("Student",  new XElement("Name", "张三"),  new XElement("Sex", "男"),  new XElement("Age", 20)),  new XElement("Student",  new XElement("Name", "李四"),  new XElement("Sex", "女"),  new XElement("Age", 19))  );  Console.WriteLine(Students);  编历XML  XElement Students = new XElement("Students",  new XElement("Student",  new XElement("Name", "张三"),  new XElement("Sex", "男"),  new XElement("Age", 20)),  new XElement("Student",  new XElement("Name", "李四"),  new XElement("Sex", "女"),  new XElement("Age", 19))  );  foreach (XNode node in Students.Nodes())  {  Console.WriteLine(node);  Console.WriteLine("----------------------------");  }  foreach (XElement ele in Students.Elements())  {  Console.WriteLine(ele);  Console.WriteLine("********************************");  }

添加XML节点

XElement Students = new XElement("Students",   new XElement("Student",  new XElement("Name", "张三"),  new XElement("Sex", "男"),  new XElement("Age", 20)),  new XElement("Student",  new XElement("Name", "李四"),  new XElement("Sex", "女"),  new XElement("Age", 19))  );  foreach (XElement ele in Students.Elements())  {  ele.Element("Age").AddAfterSelf(new XElement("Hight", 173));  ele.Element("Age").AddBeforeSelf(new XElement("Weight", 73));  ele.Add (new XElement("Hobby", "Coding"));  }  Console.WriteLine(Students)  更新XML节点  XElement Students = new XElement("Students",  new XElement("Student",  new XElement("Name", "张三"),  new XElement("Sex", "男"),  new XElement("Age",new XAttribute ("Year",1989/8/22), 20))  );  Students.Element(“Student”).Element(“Age”).ReplaceWith(new XElement(“Age”, 28));//替换掉整个节点  // Students.Element(“Student”).Element(“Age”).ReplaceNodes ( 28);//只替换节点值  // Students.Element(“Student”).Element(“Age”).ReplaceAll (28);//替换掉整个节点  Console.WriteLine(Students);  删除XML节点  XElement Students = new XElement("Students",  new XElement("Student",  new XElement("Name", "张三"),  new XElement("Sex", "男"),  new XElement("Age",new XAttribute ("Year","1989/8/22"), 20))  );  //Students.Element("Student").Element("Age").Remove ();//移除节点  //Students.Element("Student").Element("Age").RemoveAll();//移除节点的值和属性  Students.Element("Student").Element("Age").RemoveNodes();//移除节点的值  Console.WriteLine(Students);  添加XML属性  XElement Students = new XElement("Students",  new XElement("Student",  new XElement("Name", "张三"),  new XElement("Sex", "男"),  new XElement("Age",new XAttribute ("Year","1989/8/22"), 20))  );  Students.Element("Student").SetAttributeValue("dd","dddd");  Console.WriteLine(Students);  更新XML属性  Students.Element("Student").Element("Age").ReplaceAttributes(new XAttribute("Year","dd"));  Students.Element("Student").Element("Age").SetAttributeValue("Year", "dddd");  删除XML属性  Students.Element("Student").Element("Age").Attribute("Year").Remove ();  Students.Element("Student").Element("Age").RemoveAttributes ();  遍历XML属性  var Attr = from att in Students.Element("Student").Element("Age").Attributes()  select att;  foreach (var att in Attr)  {  Console.WriteLine(att);  }

感谢各位的阅读,以上就是“LINQ To XML怎么使用”的内容了,经过本文的学习后,相信大家对LINQ To XML怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网页标题:LINQToXML怎么使用
转载源于:http://kswsj.com/article/pidcii.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号