在向大家详细介绍LINQ To XML——XML操作之前,首先让大家了解下LINQ to XML框架是一个轻量级的XML编程API,然后全面介绍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);
- }
【编辑推荐】