温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LINQ To XML怎么使用

发布时间:2021-12-02 09:40:41 来源:亿速云 阅读:111 作者:iii 栏目:编程语言

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

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怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI