-
C#教程之C# LINQ to XML应用介绍
W3C制定了XML DOM标准,.Net为了支持W3C的标准,从1.1版本开始就引入了XmlDocument类。我在前一篇博客中,介绍了如何使用XmlDocument类来对XML文档进行操作。后来 .Net又引入了LINQ,于是LINQ to XML也就应运而生,所以在.Net中,不仅可以用W3C XML DOM标准,还可以使用LINQ to XML来操作XML文档。下面就来简单介绍一下如何使用LINQ to XML。
(一) 加载
加载XML比较常用的有三种方法:
public static XDocument Load(string uri);
public static XDocument Load(Stream stream);
public static XDocument Parse(string text);
下面代码演示如何使用它们:
// public static XDocument Load(string uri);
// uri 即为要装载的文件名
var doc1 = XDocument.Load("XMLFile1.xml");
// public static XDocument Load(Stream stream);
Entity retrievedAnnotation = _orgService.Retrieve("annotation"
, new Guid("C1B13C7F-F430-E211-8FA1-984BE1731399"), new ColumnSet(true));
byte[] fileContent = Convert.FromBase64String(retrievedAnnotation["documentbody"].ToString());
MemoryStream ms = new MemoryStream(fileContent);
XDocument xDoc = XDocument.Load(ms);
// public static XDocument Parse(string text);
string str = @"<Customers><Customer id='01' city='Beijing' country='China' name='Lenovo'/></Customers>";
var doc2 = XDocument.Parse(str);
(二) 查询
我们以下面的XML文档为例:
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<Customer id="01" city="Beijing" country="China">Lenovo
<Order OrderID="1001" Freight="36.00" />
<Order OrderID="1003" Freight="61.50" />
</Customer>
<Customer id="02" city="Amsterdam" country="The Netherlands">Shell
<Order OrderID="1002" Freight="56.65" />
<Order OrderID="1004" Freight="65.50" />
<Order OrderID="1005" Freight="100.50" />
</Customer>
</Customers>
1. 返回所有Customer 节点:
var result = from customer in doc1.Descendants("Customer")
select customer.Value;
foreach (var s in result)
{
Console.WriteLine(s);
}
输出结果:
Lenovo
Shell
2. 返回id为02并且 city 为 Amsterdam 的customer :
var result = (from customer in doc1.Descendants("Customer")
where (string)customer.Attribute("id") == "02" && (string)customer.Attribute("city") == "Amsterdam"
select customer.Value).FirstOrDefault();
Console.WriteLine(result);
输出结果:
Shell
3. 查找出 order ID 1003的customer ID和它的freight:
var result = (from order in doc1.Descendants("Order")
where order.Attribute("OrderID").Value == "1003"
select new
{
CustomerID = order.Parent.Attribute("id").Value,
Freight = (decimal)order.Attribute("Freight")
}).FirstOrDefault();
Console.WriteLine(string.Format("Customer ID: {0} Freight: {1}", result.CustomerID, result.Freight));
输出结果:
Customer ID: 01 Freight: 61.50
4. 查询每个客户的freight的总和
var result = from customer in doc1.Descendants("Customer")
select new
{
CustomerName = customer.Value,
TotalFreight = customer.Descendants("Order").Sum(o => (decimal)o.Attribute("Freight"))
};
foreach (var r in result)
{
Console.WriteLine(string.Format("Customer: {0} Total Freight: {1}", r.CustomerName, r.TotalFreight));
}
输出结果:
Customer: Lenovo Total Freight: 97.50
Customer: Shell Total Freight: 222.65
5. 使用LINQ to XML Join
Join可以用在LINQ to XML和其他的LINQ providers,比如说LINQ to Objects。下面的代码展示了如何将一个数组和一个XML文件Join起来。
string[] orders = {"1001", "2000", "1002"};
var result = from order in doc1.Descendants("Order")
join selected in orders
on (string)order.Attribute("OrderID") equals selected
select new
{
CustomerName = order.Parent.Value,
OrderID = selected,
Freight = (decimal)(order.Attribute("Freight"))
};
foreach (var r in result)
{
Console.WriteLine(string.Format("Customer ID: {0} Order:{1} Freight: {2}", r.CustomerName, r.OrderID, r.Freight));
}
输出结果:
Customer ID: Lenovo Order:1001 Freight: 36,00
Customer ID: Shell Order:1002 Freight: 56,65
(三) 创建
以创建以下XML文档为例:
<?xml version="1.0" encoding="utf-8"?>
<Customers>
<Customer id="01" city="Beijing" country="China" name="Lenovo">
<Order OrderID="1001" Freight="36.00" />
</Customer>
</Customers>
var doc = new XDocument(
new XElement("Customers",
new XElement("Customer",
new XAttribute("id", "01"),
new XAttribute("city", "Beijing"),
new XAttribute("country", "China"),
new XAttribute("name", "Lenovo"),
new XElement("Order",
new XAttribute("OrderID", "1001"),
new XAttribute("Freight", "36.00")
)
)
)
);
doc.Save("test.xml");
总结:
1. XDocument提供了对XML文档在内存中的随机的读写操作。
2. XDocument使用LINQ to XML来读取XML结点。
3. 你可以通过LINQ投射(projection)来将XML变换为Object。
4. LINQ投射可以将XML变换为IEnumerable<String>。
5. LINQ投射可以将XML变换为其他格式的XML。
(一) 加载
加载XML比较常用的有三种方法:
复制代码 代码如下:
public static XDocument Load(string uri);
public static XDocument Load(Stream stream);
public static XDocument Parse(string text);
下面代码演示如何使用它们:
复制代码 代码如下:
// public static XDocument Load(string uri);
// uri 即为要装载的文件名
var doc1 = XDocument.Load("XMLFile1.xml");
// public static XDocument Load(Stream stream);
Entity retrievedAnnotation = _orgService.Retrieve("annotation"
, new Guid("C1B13C7F-F430-E211-8FA1-984BE1731399"), new ColumnSet(true));
byte[] fileContent = Convert.FromBase64String(retrievedAnnotation["documentbody"].ToString());
MemoryStream ms = new MemoryStream(fileContent);
XDocument xDoc = XDocument.Load(ms);
// public static XDocument Parse(string text);
string str = @"<Customers><Customer id='01' city='Beijing' country='China' name='Lenovo'/></Customers>";
var doc2 = XDocument.Parse(str);
(二) 查询
我们以下面的XML文档为例:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<Customer id="01" city="Beijing" country="China">Lenovo
<Order OrderID="1001" Freight="36.00" />
<Order OrderID="1003" Freight="61.50" />
</Customer>
<Customer id="02" city="Amsterdam" country="The Netherlands">Shell
<Order OrderID="1002" Freight="56.65" />
<Order OrderID="1004" Freight="65.50" />
<Order OrderID="1005" Freight="100.50" />
</Customer>
</Customers>
1. 返回所有Customer 节点:
复制代码 代码如下:
var result = from customer in doc1.Descendants("Customer")
select customer.Value;
foreach (var s in result)
{
Console.WriteLine(s);
}
输出结果:
Lenovo
Shell
2. 返回id为02并且 city 为 Amsterdam 的customer :
复制代码 代码如下:
var result = (from customer in doc1.Descendants("Customer")
where (string)customer.Attribute("id") == "02" && (string)customer.Attribute("city") == "Amsterdam"
select customer.Value).FirstOrDefault();
Console.WriteLine(result);
输出结果:
Shell
3. 查找出 order ID 1003的customer ID和它的freight:
复制代码 代码如下:
var result = (from order in doc1.Descendants("Order")
where order.Attribute("OrderID").Value == "1003"
select new
{
CustomerID = order.Parent.Attribute("id").Value,
Freight = (decimal)order.Attribute("Freight")
}).FirstOrDefault();
Console.WriteLine(string.Format("Customer ID: {0} Freight: {1}", result.CustomerID, result.Freight));
输出结果:
Customer ID: 01 Freight: 61.50
4. 查询每个客户的freight的总和
复制代码 代码如下:
var result = from customer in doc1.Descendants("Customer")
select new
{
CustomerName = customer.Value,
TotalFreight = customer.Descendants("Order").Sum(o => (decimal)o.Attribute("Freight"))
};
foreach (var r in result)
{
Console.WriteLine(string.Format("Customer: {0} Total Freight: {1}", r.CustomerName, r.TotalFreight));
}
输出结果:
Customer: Lenovo Total Freight: 97.50
Customer: Shell Total Freight: 222.65
5. 使用LINQ to XML Join
Join可以用在LINQ to XML和其他的LINQ providers,比如说LINQ to Objects。下面的代码展示了如何将一个数组和一个XML文件Join起来。
复制代码 代码如下:
string[] orders = {"1001", "2000", "1002"};
var result = from order in doc1.Descendants("Order")
join selected in orders
on (string)order.Attribute("OrderID") equals selected
select new
{
CustomerName = order.Parent.Value,
OrderID = selected,
Freight = (decimal)(order.Attribute("Freight"))
};
foreach (var r in result)
{
Console.WriteLine(string.Format("Customer ID: {0} Order:{1} Freight: {2}", r.CustomerName, r.OrderID, r.Freight));
}
输出结果:
Customer ID: Lenovo Order:1001 Freight: 36,00
Customer ID: Shell Order:1002 Freight: 56,65
(三) 创建
以创建以下XML文档为例:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<Customers>
<Customer id="01" city="Beijing" country="China" name="Lenovo">
<Order OrderID="1001" Freight="36.00" />
</Customer>
</Customers>
复制代码 代码如下:
var doc = new XDocument(
new XElement("Customers",
new XElement("Customer",
new XAttribute("id", "01"),
new XAttribute("city", "Beijing"),
new XAttribute("country", "China"),
new XAttribute("name", "Lenovo"),
new XElement("Order",
new XAttribute("OrderID", "1001"),
new XAttribute("Freight", "36.00")
)
)
)
);
doc.Save("test.xml");
总结:
1. XDocument提供了对XML文档在内存中的随机的读写操作。
2. XDocument使用LINQ to XML来读取XML结点。
3. 你可以通过LINQ投射(projection)来将XML变换为Object。
4. LINQ投射可以将XML变换为IEnumerable<String>。
5. LINQ投射可以将XML变换为其他格式的XML。
最新更新
Objective-C语法之代码块(block)的使用
VB.NET eBook
Add-in and Automation Development In VB.NET 2003 (F
Add-in and Automation Development In VB.NET 2003 (8
Add-in and Automation Development in VB.NET 2003 (6
Add-in and Automation Development In VB.NET 2003 (5
AddIn Automation Development In VB.NET 2003 (4)
AddIn And Automation Development In VB.NET 2003 (2)
Addin and Automation Development In VB.NET 2003 (3)
AddIn And Automation Development In VB.NET 2003 (1)
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
武装你的WEBAPI-OData入门
武装你的WEBAPI-OData便捷查询
武装你的WEBAPI-OData分页查询
武装你的WEBAPI-OData资源更新Delta
5. 武装你的WEBAPI-OData使用Endpoint 05-09
武装你的WEBAPI-OData之API版本管理
武装你的WEBAPI-OData常见问题
武装你的WEBAPI-OData聚合查询
OData WebAPI实践-OData与EDM
OData WebAPI实践-Non-EDM模式