linq to xml基础
来源:互联网 发布:iphone usb共享网络 编辑:程序博客网 时间:2024/05/18 02:15
额,我们接着说linq to xml 基础,博主也只会点基础,大家就不要笑话了。博主写博客的目的是希望能帮助到有需要的人,哈哈单元没有基础到大家都不需要。下面分别用增、删、改几段代码来说怎么用linq怎么操作xml。首先引用using System.Xml.Linq;本文主要介绍XDocument 、XElement、XAttribute
1、创建xml,使用XElement来创建元素节点,XAttribute来创建属性,通过XDocument来保存到指定的位置。如下面代码所示:
1 XDocument xdocc = new XDocument(); 2 XElement root = new XElement("Root", 3 new XElement("Student", 4 new XAttribute("StudentId", "1"), 5 new XElement("UserInfro", 6 new XAttribute("UserName", "张三"), 7 new XAttribute("Sex", "男"), 8 new XAttribute("Profession", "计算机") 9 ),10 new XElement("UserLogs",11 new XAttribute("BeginTime", "2010-8-01"),12 new XAttribute("EndTime", ""),13 new XAttribute("UserInfor", "")14 )15 )16 );17 xdocc.Add(root);18 xdocc.Save("C:\\Student.xml");
直接双击打开已创建的xml结果如下:(额,我的windows8下操作的时候提示对指定路径的操作被拒绝,右键已管理员身份运行就好了)
<?xml version="1.0" encoding="UTF-8"?> <Root> <Student StudentId="1"> <UserInfro Profession="计算机" Sex="男" UserName="张三"/>
<UserLogs UserInfor="" EndTime="" BeginTime="2010-8-01"/>
</Student>
</Root>
2、修改已经创建的XML文档,使用SetAttributeValue修改属性,使用SetElementValue修改元素,保存到原来的位置。
1 string path = "C:\\Student.xml";2 XDocument xdoc = new XDocument();3 XElement root = XElement.Load(path);4 //修改属性5 root.Element("Student").Element("UserInfro").SetAttributeValue("UserName", "李四");6 //修改元素7 root.SetElementValue("UserLogs", "学生信息日志");8 xdoc.Add(root);9 xdoc.Save(path);
修改后结果如下:
<?xml version="1.0" encoding="UTF-8"?><Root> <Student StudentId="1"> <UserInfro Profession="计算机" Sex="男" UserName="李四"/><UserLogs UserInfor="" EndTime="" BeginTime="2010-8-01"/></Student><UserLogs>学生信息日志</UserLogs></Root>
3、使用Remove关键字删除已有XML文档中指定的元素或者属性。
1 string path = "C:\\Student.xml";2 XDocument xdoc = new XDocument();3 XElement root = XElement.Load(path);4 root.Element("UserLogs").Remove();5 root.Element("Student").Element("UserInfro").Attribute("Profession").Remove();6 xdoc.Add(root);7 xdoc.Save(path);
删除后结果如下:
<?xml version="1.0" encoding="UTF-8"?><Root><Student StudentId="1"> <UserInfro Sex="男" UserName="李四"/> <UserLogs UserInfor="" EndTime="" BeginTime="2010-8-01"/></Student></Root>
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- linq to xml基础
- LINQ to XML基础
- LINQ to XML 编程基础
- LINQ to XML 编程基础
- LINQ to XML 编程基础
- LINQ to XML 编程基础
- LINQ to XML 编程基础
- LINQ to XML 编程基础
- LINQ to XML 编程基础
- LINQ to XML 编程基础
- 使用LINQ to XML
- LINQ TO XML
- LINQ TO XML学习
- linq to xml demo
- LINQ to XML简介
- LINQ to XML
- linq to xml入门
- LINQ to XML
- hql的关联查询 join
- java.io.IOException: Unable to open sync connection!的解决方案
- Oracle体系结构及备份(十)——sga-others_pool
- JAVA run方法中得到线程名称
- js弹出对话框
- linq to xml基础
- js 获取服务器控件的值
- Error configuring application listener of class org.springframework.web.context.Context
- WM_NULL message
- jsp下载文件
- 【Linux基础】shell控制结构
- Windows XP硬盘安装Ubuntu 12.04双系统图文详解
- mysql 无法进行远程访问的问题
- 众核指令数据加载