你的位置:首页 > ASP.net教程

[ASP.net教程]XML文档操作集锦(C#篇)


  在JSON没流行起来的时候

文件加载

SelectNodes()、SelectSingleNode()节点获取大法

创建

DataSet与

Linq to

文件加载

  •  通过路径加载
  new //文件路径或者URL地址  Console.WriteLine("==============有逼格的分割线================");  "http://feed.cnblogs.com/blog/picked/rss");//博客园精华帖RSS  Console.WriteLine(

  Load()方法还重载了一些方法,并且还有别的方式可以加载

SelectNodes()、SelectSingleNode()节点获取大法

<??><Names> <Name>  <FirstName>John</FirstName>  <LastName>Smith</LastName> </Name> <Name>  <FirstName>James</FirstName>  <LastName>White</LastName> </Name></Names>


  • 通过XPath表达式的SelectNodes(string xpath)方法获取节点列表;SelectSingleNode(string xpath)获取第一个匹配的节点
  = "/Names/Name");//第一个斜杠必须是根节点  foreach (in "==================================");  = "/Names/Name");  Console.WriteLine("LastName"].InnerText);//获取子节点


 = "/Root/Item[@Grade='1']");//XPath表达式获取设置要获取节点的特性值信息 foreach (in 

  • 获取第N个节点
  "/Root/Item[position()=3]");//获取第N个节点(根据  Console.WriteLine("==============================");  = "/Root/Item[position()<=3]");//获取从第一个到第N个的节点(根据  foreach (in "==============================");  = "/Root/Item[position() > 2]");//获取大于第N个的节点(根据  foreach (in 

创建
  new = "Root");//创建一个节点  "==============有逼格的分割线================");  = "Item");  = "Id");//创建节点特性  attribute.Value = "1";//设置特性值  node.Attributes.Append(attribute);  node.InnerText = "张三";  rootNode.AppendChild(node);  Console.WriteLine("==============有逼格的分割线================");  node = "Item");  attribute = "Id");  attribute.Value = "2";  node.Attributes.Append(attribute);  node.InnerText = "李四";  rootNode.AppendChild(node);  Console.WriteLine(  //保存到文件


DataSet与

  通过

  using (new = new DataSet();    ds.Read= ds.Tables[0];    for (int i = 0; i < dt.Rows.Count - 1; i++)    {      object[] items = dt.Rows[i].ItemArray;      Console.WriteLine(string.Join(" ", items));    }  }


 Linq to

  通过linq操作

 //查询 XDocument xdoc = XDocument.Load(fileName); var query = from item in xdoc.Descendants("Item")       select new { Id = item.Attribute("Id").Value, Name = item.Value }; foreach (var item in query) {   Console.WriteLine(item.Id + " " + item.Name); }

 //添加 XDocument xdoc = XDocument.Load(fileName); XElement xele = new XElement("Item","吴十?");//创建节点 xele.SetAttributeValue("Id", 10);//设置节点特性值 xdoc.Element("Root").Add(xele); xdoc.Save(fileName);

 //删除 XDocument xdoc = XDocument.Load(fileName); XElement xele = xdoc.Descendants("Item").Where(c => c.Attribute("Id").Value.Equals("10")).FirstOrDefault(); xele.Remove(); xdoc.Save(fileName);

 //修改 XDocument xdoc = XDocument.Load(fileName); XElement xele = xdoc.Descendants("Item").Where(c => c.Attribute("Id").Value.Equals("1")).FirstOrDefault(); xele.Value = "流年"; xdoc.Save(fileName);

 //序列化 var student = new Student() { Id = 1, Grade = 2, Name = "王五" }; = new typeof(Student)); serializer.Serialize(Console.Out, student);

 //反序列化 string input = "<?"; using (StringReader sr = new StringReader(input)) {   = new typeof(Student));   Student student = (Student)serializer.Deserialize(sr);   Console.WriteLine(student.Name); }

扩展阅读

http://www.dotnetcurry.com/linq/564/linq-to-

http://www.tutorialspoint.com/linq/linq_

https://msdn.microsoft.com/en-us/library/mt693062.aspx

http://broadcast.oreilly.com/2010/10/understanding-c-simple-linq-to.html

http://www.codeproject.com/Articles/24376/LINQ-to-

http://csharp.net-tutorials.com/

https://msdn.microsoft.com/zh-cn/library/58a18dwa(v=vs.120).aspx

http://csharp.net-informations.com/