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>
原创粉丝点击