C# Linq To XML的学习(创建并编辑XML树)示例
2008-06-30 16:53
471 查看
用Linq创建xml树,并对树中元素或属性的值修改。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
namespace LinqToXMLStudy
{
//***********************************************************************************************************
//Module:Program.cs
//Author:factly
//Create Date:2008-06-30
//***********************************************************************************************************
class Program
{
static void Main(string[] args)
{
//用linq创建XML树------------------------------------------------------------------------------------
XElement xmlTree = new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144",
new XAttribute("Type", "Home")),
new XElement("Phone", "425-555-0145",
new XAttribute("Type", "Work")),
new XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
)
);
Console.WriteLine(xmlTree);
//用linq创建XML树结束-------------------------------------------------------------------------------
//修改xml中属性的值---------------------------------------------------------------------------------
IEnumerable<System.Xml.Linq.XElement> xe = xmlTree.Descendants("Phone");//查询出元素名为Phone的所有集合,查询时区分大小写
var v = xe.Where(p => p.Attribute("Type").Value == "Home");
foreach (var s in v)
{
s.SetValue("hello");
}
Console.WriteLine("修改后的结果为:");
Console.WriteLine(xmlTree);
//修改xml中属性的值结束-----------------------------------------------------------------------------
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
namespace LinqToXMLStudy
{
//***********************************************************************************************************
//Module:Program.cs
//Author:factly
//Create Date:2008-06-30
//***********************************************************************************************************
class Program
{
static void Main(string[] args)
{
//用linq创建XML树------------------------------------------------------------------------------------
XElement xmlTree = new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144",
new XAttribute("Type", "Home")),
new XElement("Phone", "425-555-0145",
new XAttribute("Type", "Work")),
new XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
)
);
Console.WriteLine(xmlTree);
//用linq创建XML树结束-------------------------------------------------------------------------------
//修改xml中属性的值---------------------------------------------------------------------------------
IEnumerable<System.Xml.Linq.XElement> xe = xmlTree.Descendants("Phone");//查询出元素名为Phone的所有集合,查询时区分大小写
var v = xe.Where(p => p.Attribute("Type").Value == "Home");
foreach (var s in v)
{
s.SetValue("hello");
}
Console.WriteLine("修改后的结果为:");
Console.WriteLine(xmlTree);
//修改xml中属性的值结束-----------------------------------------------------------------------------
Console.ReadKey();
}
}
}
相关文章推荐
- C# Linq To XML的学习(创建并编辑XML树)示例
- 参考文章-C# Linq To XML的学习(创建并编辑XML树)示例
- C#使用linq to xml完成对XML文件的创建、增加、删除、修改、查询
- C#学习之Linq to Xml
- C#示例学习(八)-Linq to object(5)分页效果
- C#中linq学习(将泛型集合转换为XML元素)示例
- C#基础巩固(2)-Linq To XML创建XML
- C#学习之Linq to Xml
- C# Linq to xml学习 用XElement类读写XML文档(用作配置文档)
- C#中linq学习(将泛型集合转换为XML元素)示例
- C# LINQ to XML示例
- C#新手入门代码 LINQ TO XML xattribute的用法示例
- C# 2008 学习笔记 - LINQ to XML
- [代码]如何从XmlReader创建XML树(LINQ to XML)
- C#使用Linq To XML读取XML,Linq生成XML,Linq创建带属性或带节点XML
- C# 2008 学习笔记 - LINQ to XML
- C# 2008 学习笔记 - LINQ to XML
- C# 2008 学习笔记 - LINQ to XML
- C# 2008 学习笔记 - LINQ to XML
- Linq To Xml 创建修改xml文档