LINQ to XML 提供了改进的 XML 编程接口,这一点可能与 LINQ to XML 的 LINQ 功能同样重要。 通过 LINQ to XML,对 XML 编程时,您可以实现任何预期的操作,包括:
-
从文件或流加载 XML。
-
将 XML 序列化为文件或流。
-
使用函数构造从头开始创建 XML。
-
使用类似 XPath 的轴查询 XML。
-
使用 Add、Remove、ReplaceWith 和 SetValue 等方法对内存 XML 树进行操作。
-
使用 XSD 验证 XML 树。
-
使用这些功能的组合,可将 XML 树从一种形状转换为另一种形状。
1、读取XML文档
从文件中加载:
XElement root = XElement.Load(path);
从内存中加载:
XElement root = XElement.Parse(@"<Categories>
<Category>
<CategoryID>1</CategoryID>
<CategoryName>Beverages</CategoryName>
<Description>Soft drinks, coffees, teas, beers, and ales</Description>
</Category>
</Categories>
");
2、基本操作:添加、删除、修改、保存节点操作
添加:
-
public static void AddAfterSelf()
-
{
-
XElement root = XElement.Parse(@"
-
-
<Categories>
-
-
<Category>
-
-
<CategoryID>1</CategoryID>
-
-
<CategoryName>Beverages</CategoryName>
-
-
<Description>Soft drinks, coffees, teas, beers, and ales</Description>
-
-
</Category>
-
-
</Categories>
-
-
");
-
XElement xele = root.Element("Category").Element("CategoryName");
-
xele.AddAfterSelf(new XElement("AddDate", DateTime.Now));
-
root.Save(path);
-
}
修改:
-
public static void Update()
-
{
-
XElement root = XElement.Parse(@"
-
<Categories>
-
<Category>
-
<CategoryID>1</CategoryID>
-
<CategoryName>Beverages</CategoryName>
-
<Description>Soft drinks, coffees, teas, beers, and ales</Description>
-
</Category>
-
</Categories>
-
");
-
root.Element("Category").Element("CategoryID").ReplaceWith(new XElement("ID", "2"));
-
root.Element("Category").SetElementValue("CategoryName", "test data");
-
root.Save(path);
-
}
删除:
-
public static void Remove()
-
{
-
string path = @"d:\";
-
XElement root = XElement.Parse(@"
-
<Categories>
-
-
<Category>
-
-
<CategoryID>1</CategoryID>
-
-
<CategoryName>Beverages</CategoryName>
-
-
<Description>Soft drinks, coffees, teas, beers, and ales</Description>
-
-
</Category>
-
-
</Categories>
-
-
");
-
root.RemoveAll();
-
root.Save(path);
-
}
添加属性
-
public static void AddAttribute()
-
{
-
XElement root = new XElement("Categories",
-
new XElement("Category",
-
new XAttribute("CategoryID", "1"),
-
new XElement("CategoryName", "Beverages"),
-
new XElement("Description", "Soft drinks, coffees, teas, beers, and ales")
-
)
-
);
-
root.Element("Category").Add(new XAttribute("AddDate", DateTime.Now.ToShortDateString()));
-
root.Save(path);
-
}
3、查询语法
检索属性:
-
public static void SelectAttribute()
-
{
-
XElement root = new XElement("Categories",
-
new XElement("Category",
-
new XAttribute("CategoryID", "1"),
-
new XElement("CategoryName", "Beverages"),
-
new XElement("Description", "Soft drinks, coffees, teas, beers, and ales")
-
)
-
);
-
XAttribute xattr = root.Element("Category").Attribute("CategoryID");
-
Console.WriteLine(xattr.Name);
-
Console.WriteLine(xattr.Value);
-
}
遍历:
-
public static void Enum()
-
{
-
XElement root = new XElement("Categories");
-
using (NorthwindDataContext db = new NorthwindDataContext())
-
{
-
root.Add(
-
db.Categories
-
.Select
-
(
-
c => new XElement
-
-
(
-
"Category"
-
-
, new XElement("CategoryName", c.CategoryName)
-
)
-
)
-
);
-
}
-
foreach (var item in root.Elements("Category"))
-
{
-
Console.WriteLine(item.Element("CategoryName").Value);
-
}
-
}
4、加载复杂对象
var rooms = from room in doc.Descendants("Room")
select new
{
RoomID = room.Element("RoomID").Value,
Prices = from price in XDocument.Parse(room.ToString()).Descendants("Price")
select new
{
PriceCode = price.Element("PriceCode").Value,
Stays = from stay in XDocument.Parse(price.ToString()).Descendants("Stay")
select new
{
Date = stay.Element("Date").Value,
Rate = stay.Element("Rate").Value,
RoomStatus = stay.Element("RoomStatus").Value,
BreakfastType = stay.Element("BreakfastType").Value
}
}
};