我们的配置文件使用XML存储信息。ADO.NET的DataSet(利用扩展方法)可以方便的将数据保存(或加载)为XML。.NET特有的XML API,如XmlReader/XmlWriter类。微端提供了一个特殊的程序集 System.Xml.dll专门对XML文档进行编程。因为LINQ操作数据比较方便,所有大多数人选择使用Linq to XML API来于XML数据进行交互。

传统的XML写法

XmlElement类的介绍

这个用法就跟用JavaScript在页面上创建元素一样的

            //在内存中新建一个Xml文档
XmlDocument doc = new XmlDocument(); //穿件元素 <inventory>
XmlElement inventory = doc.CreateElement("Inventory"); //创建<Car>子元素 包含一个ID特性
XmlElement car = doc.CreateElement("Car");
car.SetAttribute("ID", ""); //创建Car元素中的数据
XmlElement name = doc.CreateElement("PetName");
name.InnerText = "Jimbo";
XmlElement color = doc.CreateElement("Color");
color.InnerText = "Red";
XmlElement make = doc.CreateElement("Make");
make.InnerText = "Ford"; //将<PetName><Color><Make>
car.AppendChild(name);
car.AppendChild(color);
car.AppendChild(make); //将<Car>元素添加到<Inventory>元素
inventory.AppendChild(car); //将完整的XML插入到XmlDocument对象并保存文件
doc.AppendChild(inventory);
doc.Save("Inventory.xml");

更优秀的Dom———Linq to XML

System.XML.Linq包含了一组非常易于管理的类,它们代表一个XML文档的不同方面(元素、属性、XML命名控件、XML注释、处理指令等)

    //注意把格式调一下,可以看的很清除
XElement doc = new XElement("Inventory",
new XElement("Car", new XAttribute("ID", ""),
new XElement("PetName", "Jimbo"),
new XElement("Color", "Red"),
new XElement("Color", "Ford")
)
);

这样对比一下,下面的一种更简些。

System.Xml.Linq 命名空间的成员

LinQ to XML的核心程序集(System.Xml.Linq.dll)只在三个不同空间下定义了很少的类型。这 三个命名空间是 System.Xml.Linq、System.Xml.Schema、System.Xml.xPath.

System.Xml.Linq命名空间的成员

LINQ to XML的轴方法

除这些X*类,System.Xml.Linq中还定义了一个名为Extensions的类,它还定义了一组针对IEnumerable<T>的扩展方法,其中为XNode或XContainer的子类。

LINQ to XML的Extensions类的Selecr成员

这些方法允许在一个已加载的XML树中进行查询。以查找元素、特性以及他们的值。这么方法被称为轴方法(axis method)或简称轴。抽象类XContainer类支持很多于Extensions方法的名称相同的方法。由于XContainer是XElement和XDocument的父类,所以都支持全部的功能。

例如:

  XElement doc = new XElement("Inventory",
new XElement("Car", new XAttribute("ID", ""),
new XElement("PetName", "Jimbo"),
new XElement("Color", "Red"),
new XElement("Color", "Ford")
)
); doc.Descendants("PetName").Remove(); //先找到该元素再删除,Linq一样
doc.Save("InventoryWithLinq1.xml");

奇妙的XName和XNamespace

Desendants<XName name>

相反,此类提供了隐式转换 String ,允许您创建 XName.他会将一个简单的System.String映射到正确的XName对象。XNamespace一样。

使用XElement和XDocument

XDocument表示整个XML文档。它可以用来定义一个根元素及其包含的所有元素、处理指令和XML声明。

            XDocument inventroyDoc = new XDocument(
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Current Inventroy of cars"),
new XProcessingInstruction("xml-stylesheet", "href='MyStyless.css' title='Compact' type='text/css'"),
new XElement("Inventory",
new XElement("Car", new XAttribute("ID", ""),
new XElement("Color", "Green"),
new XElement("Make", "BWM"),
new XElement("PetName", "stan")
),
new XElement("Car", new XAttribute("ID", ""),
new XElement("Color", "Pink"),
new XElement("Make", "Yugo"),
new XElement("PetName", "Melvin")
) )
));
inventroyDoc.Save("test.xml");

可以简单的就用XElement来创建XML元素。

从数组和容器中生成文档

读取数据身材XElement(或XDocument).

//创建一个匿名类型的数组
var people = new[]{
new {FirstName="Mandy",Age=},
new {FirstName="Andrew",Age=},
new {FirstName="Dave",Age=},
new {FirstName="Sara",Age=},
}; XElement peopleDoc = new XElement("Perple", from c in people select new XElement("Person", new XAttribute("Age", c.Age), new XElement("FirstName", c.FirstName))
); //或者 都会得到相同的结果
var arrayDataAdsXlements = from c in people select new XElement("Person", new XAttribute("Age", c.Age), new XElement("FirstName", c.FirstName));
XElement peopleDoc2 = new XElement("People", arrayDataAdsXlements); peopleDoc.Save("one.xml");
peopleDoc2.Save("two.xml");

加载和解析XML内容

XElement和XDocument都支持Load()和Parse()方法,可以从包含XML数据的string对象或外部XML文件中获取XML对象模型。

 string myElement =
@"<Car ID='3'>
<Color>Yellow</Color>
<Make>Yugo</Make>
</Car>";
XElement newElement = XElement.Parse(myElement);
Console.WriteLine(newElement); Console.WriteLine();
XDocument myDoc = XDocument.Load("one.xml");
Console.WriteLine(myDoc);

定义LINQ to  XML辅助类

public static void InsertNewElement(string make, string color, string petName)   //一个添加的方法
{
XDocument inventoryDoc = XDocument.Load("test.xml");
Random r = new Random();
XElement newElement = new XElement("Car",
new XAttribute("ID", r.Next()),
new XElement("Color", color),
new XElement("Make", make),
new XElement("PetName", petName)); inventoryDoc.Descendants("Inventory").First().Add(newElement);
inventoryDoc.Save("test.xml");
}

原:

运行该方法:

查询:

最新文章

  1. Task异步编程
  2. EditText 几种显示方式,固定行数,自适应行数
  3. 跟着百度学PHP[2]-foreach条件嵌套
  4. iOS开发——高级技术&amp;地图功能的实现
  5. 分布式人工智能标记语言(DAIML)示例
  6. java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim [问题点数:40分,结帖人wangxiaohua_001]
  7. .NET下用C#实现邮箱激活功能
  8. [UML]UML之开篇
  9. MySql的大小写问题
  10. iOS开发——OC篇&amp;特殊数据类型
  11. JVM调优总结(一)-- 一些概念
  12. 二叉树终极教程--BinarySearchTree
  13. Python 文件读写的三种模式和区别
  14. 在 Web 页面使用 VLC 插件播放 m3u8 视频流 (360 极速模式)
  15. Kotlin基础(三)类、对象和接口
  16. 在WPF中调用文件夹浏览/选择对话框
  17. word embeddding和keras中的embedding
  18. Spark注册UDF函数,用于DataFrame DSL or SQL
  19. 为 hexo NexT 添加 Gitment 评论插件
  20. 【DB】部分MySQL操作记录

热门文章

  1. [Activator-HelloAkka] Define our Actors
  2. java中HashMap的keySet()和values()
  3. 内行看门道:看似“佛系”的《QQ炫舞手游》,背后的音频技术一点都不简单
  4. ACdream 1098——圆有点挤——————【数学计算】
  5. SQLAlchemy基本操作和常用技巧
  6. 线程中断方法interrupt() 与 cancel()
  7. 在Android源码中如何吧so库打包编译进入apk, 集成第三方库(jar和so库)
  8. Java集合篇一:ArrayList
  9. SQLAlchemy的使用---增删改查
  10. Android之自定义(上方标题随ViewPager手势慢慢滑动)