最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# LINQ to XML應(yīng)用介紹

 更新時(shí)間:2012年11月21日 10:55:57   作者:  
.Net又引入了LINQ,于是LINQ to XML也就應(yīng)運(yùn)而生,所以在.Net中,不僅可以用W3C XML DOM標(biāo)準(zhǔn),還可以使用LINQ to XML來操作XML文檔。下面就來簡單介紹一下如何使用LINQ to XML
W3C制定了XML DOM標(biāo)準(zhǔn),.Net為了支持W3C的標(biāo)準(zhǔn),從1.1版本開始就引入了XmlDocument類。我在前一篇博客中,介紹了如何使用XmlDocument類來對XML文檔進(jìn)行操作。后來 .Net又引入了LINQ,于是LINQ to XML也就應(yīng)運(yùn)而生,所以在.Net中,不僅可以用W3C XML DOM標(biāo)準(zhǔn),還可以使用LINQ to XML來操作XML文檔。下面就來簡單介紹一下如何使用LINQ to XML。
(一) 加載
加載XML比較常用的有三種方法:
復(fù)制代碼 代碼如下:

public static XDocument Load(string uri);
public static XDocument Load(Stream stream);
public static XDocument Parse(string text);

下面代碼演示如何使用它們:
復(fù)制代碼 代碼如下:

// 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文檔為例:
復(fù)制代碼 代碼如下:

<?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 節(jié)點(diǎn):
復(fù)制代碼 代碼如下:

var result = from customer in doc1.Descendants("Customer")
select customer.Value;
foreach (var s in result)
{
Console.WriteLine(s);
}

輸出結(jié)果:
Lenovo
Shell
2. 返回id為02并且 city 為 Amsterdam 的customer :
復(fù)制代碼 代碼如下:

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);

輸出結(jié)果:
Shell
3. 查找出 order ID 1003的customer ID和它的freight:
復(fù)制代碼 代碼如下:

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));

輸出結(jié)果:
Customer ID: 01 Freight: 61.50
4. 查詢每個(gè)客戶的freight的總和
復(fù)制代碼 代碼如下:

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));
}

輸出結(jié)果:
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。下面的代碼展示了如何將一個(gè)數(shù)組和一個(gè)XML文件Join起來。
復(fù)制代碼 代碼如下:

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));
}

輸出結(jié)果:
Customer ID: Lenovo Order:1001 Freight: 36,00
Customer ID: Shell Order:1002 Freight: 56,65
(三) 創(chuàng)建
以創(chuàng)建以下XML文檔為例:
復(fù)制代碼 代碼如下:

<?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>

復(fù)制代碼 代碼如下:

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");

總結(jié):
1. XDocument提供了對XML文檔在內(nèi)存中的隨機(jī)的讀寫操作。
2. XDocument使用LINQ to XML來讀取XML結(jié)點(diǎn)。
3. 你可以通過LINQ投射(projection)來將XML變換為Object。
4. LINQ投射可以將XML變換為IEnumerable<String>。
5. LINQ投射可以將XML變換為其他格式的XML。

相關(guān)文章

  • C# 遞歸算法詳解

    C# 遞歸算法詳解

    什么是遞歸函數(shù)/方法?任何一個(gè)方法既可以調(diào)用其他方法也可以調(diào)用自己,而當(dāng)這個(gè)方法調(diào)用自己時(shí),我們就叫它遞歸函數(shù)或遞歸算法,接下來詳細(xì)介紹需要了解的朋友可以參考下
    2021-11-11
  • C#使用this關(guān)鍵字實(shí)現(xiàn)串聯(lián)構(gòu)造函數(shù)調(diào)用方法

    C#使用this關(guān)鍵字實(shí)現(xiàn)串聯(lián)構(gòu)造函數(shù)調(diào)用方法

    這篇文章主要介紹了C#使用this關(guān)鍵字實(shí)現(xiàn)串聯(lián)構(gòu)造函數(shù)調(diào)用方法,實(shí)例分析了使用this關(guān)鍵字串聯(lián)構(gòu)造函數(shù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • 訪問修飾符(C# 編程指南)

    訪問修飾符(C# 編程指南)

    所有類型和類型成員都具有可訪問性級別,用來控制是否可以在您程序集的其他代碼中或其他程序集中使用它們。您在聲明類型或成員時(shí)使用以下訪問修飾符之一來指定其可訪問性
    2011-02-02
  • 5分鐘用C#實(shí)現(xiàn)串口助手

    5分鐘用C#實(shí)現(xiàn)串口助手

    本文主要介紹了C#實(shí)現(xiàn)串口助手,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • C#生成EMF矢量圖形文件示例詳解

    C#生成EMF矢量圖形文件示例詳解

    這篇文章主要為大家介紹了C#?生成?EMF矢量圖形文件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • C#簡單了解接口(Interface)使用方法

    C#簡單了解接口(Interface)使用方法

    這篇文章主要介紹了C#簡單了解接口(Interface)使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • C#實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建接口并調(diào)用的實(shí)例

    C#實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建接口并調(diào)用的實(shí)例

    這篇文章介紹了C#實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建接口并調(diào)用,文中通過實(shí)例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • C#中結(jié)構(gòu)(struct)的部分初始化和完全初始化實(shí)例分析

    C#中結(jié)構(gòu)(struct)的部分初始化和完全初始化實(shí)例分析

    這篇文章主要介紹了C#中結(jié)構(gòu)(struct)的部分初始化和完全初始化,通過實(shí)例分析了結(jié)構(gòu)初始化中常見的錯(cuò)誤及技巧,有助于加深對C#結(jié)構(gòu)(struct)的認(rèn)識(shí),需要的朋友可以參考下
    2014-09-09
  • C#之如何實(shí)現(xiàn)多個(gè)子窗體切換效果

    C#之如何實(shí)現(xiàn)多個(gè)子窗體切換效果

    這篇文章主要介紹了C#之如何實(shí)現(xiàn)多個(gè)子窗體切換的效果,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • C#使用LINQ查詢操作符實(shí)例代碼(一)

    C#使用LINQ查詢操作符實(shí)例代碼(一)

    這篇文章介紹了C#使用LINQ查詢操作符的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06

最新評論

扶绥县| 莆田市| 兴城市| 永川市| 承德县| 潞城市| 隆林| 青河县| 贵德县| 云龙县| 六枝特区| 米泉市| 稻城县| 荆门市| 兰西县| 横山县| 江川县| 内江市| 东宁县| 遵义市| 翁源县| 崇礼县| 云梦县| 高陵县| 乌鲁木齐市| 焉耆| 盐池县| 宜黄县| 荃湾区| 安仁县| 通辽市| 西安市| 松江区| 阳春市| 临漳县| 桂林市| 曲沃县| 蒙城县| 杂多县| 黄大仙区| 东台市|