解析linq to xml操作XML的示例分析
更新時(shí)間:2013年05月18日 17:19:50 作者:
本篇文章是對(duì)linq to xml操作XML的示例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
.Net中的System.Xml.Linq命名空間提供了linq to xml的支持。這個(gè)命名空間中的XDocument,XElement以及XText,XAttribute提供了讀寫xml文檔的關(guān)鍵方法。
1. 使用linq to xml寫xml:
使用XDocument的構(gòu)造函數(shù)可以構(gòu)造一個(gè)Xml文檔對(duì)象;使用XElement對(duì)象可以構(gòu)造一個(gè)xml節(jié)點(diǎn)元素,使用XAttribute構(gòu)造函數(shù)可以構(gòu)造元素的屬性;使用XText構(gòu)造函數(shù)可以構(gòu)造節(jié)點(diǎn)內(nèi)的文本。
如下實(shí)例代碼:
class Program
{
static void Main(string[] args)
{
var xDoc = new XDocument(new XElement( "root",
new XElement("dog",
new XText("dog said black is a beautify color"),
new XAttribute("color", "black")),
new XElement("cat"),
new XElement("pig", "pig is great")));
//xDoc輸出xml的encoding是系統(tǒng)默認(rèn)編碼,對(duì)于簡(jiǎn)體中文操作系統(tǒng)是gb2312
//默認(rèn)是縮進(jìn)格式化的xml,而無(wú)須格式化設(shè)置
xDoc.Save(Console.Out);
Console.Read();
}
}
上面代碼將輸出如下Xml:
<?xml version="1.0" encoding="gb2312"?>
<root>
<dog color="black">dog said black is a beautify color</dog>
<cat />
<pig>pig is great</pig>
</root>
可以看出linq to xml比XmlDocument和XmlWriter要方便很多。
2. 使用linq to xml 讀取xml
Linq是從集合中查詢對(duì)象,在linq to xml中的集合是通過(guò)XElement的Elements(),Elements(string name),以及Descendants、DescendantsAndSelf、Ancestors、AncestorsAndSelf的幾個(gè)重載方法中獲得。
獲得XElement集合之后,可以通過(guò)XElement的Attribute(string name)方法獲得元素的屬性值,可以通過(guò)XElement的Value屬性獲得節(jié)點(diǎn)的文本值;使用linq就可以方便的做查詢,做篩選排序了
還是上例中的xml,我們要讀取root的所有字節(jié)點(diǎn),并打印出來(lái),如下代碼:
class Program
{
static void Main(string[] args)
{
var xDoc = new XDocument(new XElement( "root",
new XElement("dog",
new XText("dog said black is a beautify color"),
new XAttribute("color", "black")),
new XElement("cat"),
new XElement("pig", "pig is great")));
//xDoc輸出xml的encoding是系統(tǒng)默認(rèn)編碼,對(duì)于簡(jiǎn)體中文操作系統(tǒng)是gb2312
//默認(rèn)是縮進(jìn)格式化的xml,而無(wú)須格式化設(shè)置
xDoc.Save(Console.Out);
Console.WriteLine();
var query = from item in xDoc.Element( "root").Elements()
select new
{
TypeName = item.Name,
Saying = item.Value,
Color = item.Attribute("color") == null?(string)null:item.Attribute("color").Value
};
foreach (var item in query)
{
Console.WriteLine("{0} 's color is {1},{0} said {2}",item.TypeName,item.Color??"Unknown",item.Saying??"nothing");
}
Console.Read();
}
}
3. Linq to xml簡(jiǎn)單的應(yīng)用
應(yīng)用需求: 讀取博客園的rss,然后在頁(yè)面上輸出最新的10篇博客信息
實(shí)現(xiàn)要點(diǎn): 通過(guò)XDocument的Load靜態(tài)方法載入Xml,通過(guò)linq查詢最新10條數(shù)據(jù)
代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
//實(shí)際應(yīng)用,通過(guò)讀取博客園的RSS生成Html代碼顯示最新的博客列表
//使用XDocument的Load靜態(tài)方法載入Xml
var rssXDoc = XDocument.Load("http://www.dhdzp.com");
//使用linq to xml查詢前10條新博客
var queryBlogs = (from blog in rssXDoc.Descendants("item")
select new
{
Title = blog.Element("title").Value,
Url = blog.Element("link").Value,
PostTime = DateTime.Parse(blog.Element("pubDate").Value)
}).Take(20);
repeaterBlogs.DataSource = queryBlogs;
repeaterBlogs.DataBind();
base.OnLoad(e);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Linq to Xml 實(shí)例</title>
</head>
<body>
<ol>
<asp:Repeater ID="repeaterBlogs" EnableViewState="false" runat="server">
<ItemTemplate>
<li><span style="float: right">
<%#Eval("PostTime") %></span><a href="<%#Eval("Url") %>"><%#Eval("Title") %></a></li>
</ItemTemplate>
</asp:Repeater>
</ol>
</body>
</html>
C#的發(fā)展讓讀寫Xml越來(lái)越簡(jiǎn)單了。
1. 使用linq to xml寫xml:
使用XDocument的構(gòu)造函數(shù)可以構(gòu)造一個(gè)Xml文檔對(duì)象;使用XElement對(duì)象可以構(gòu)造一個(gè)xml節(jié)點(diǎn)元素,使用XAttribute構(gòu)造函數(shù)可以構(gòu)造元素的屬性;使用XText構(gòu)造函數(shù)可以構(gòu)造節(jié)點(diǎn)內(nèi)的文本。
如下實(shí)例代碼:
復(fù)制代碼 代碼如下:
class Program
{
static void Main(string[] args)
{
var xDoc = new XDocument(new XElement( "root",
new XElement("dog",
new XText("dog said black is a beautify color"),
new XAttribute("color", "black")),
new XElement("cat"),
new XElement("pig", "pig is great")));
//xDoc輸出xml的encoding是系統(tǒng)默認(rèn)編碼,對(duì)于簡(jiǎn)體中文操作系統(tǒng)是gb2312
//默認(rèn)是縮進(jìn)格式化的xml,而無(wú)須格式化設(shè)置
xDoc.Save(Console.Out);
Console.Read();
}
}
上面代碼將輸出如下Xml:
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="gb2312"?>
<root>
<dog color="black">dog said black is a beautify color</dog>
<cat />
<pig>pig is great</pig>
</root>
可以看出linq to xml比XmlDocument和XmlWriter要方便很多。
2. 使用linq to xml 讀取xml
Linq是從集合中查詢對(duì)象,在linq to xml中的集合是通過(guò)XElement的Elements(),Elements(string name),以及Descendants、DescendantsAndSelf、Ancestors、AncestorsAndSelf的幾個(gè)重載方法中獲得。
獲得XElement集合之后,可以通過(guò)XElement的Attribute(string name)方法獲得元素的屬性值,可以通過(guò)XElement的Value屬性獲得節(jié)點(diǎn)的文本值;使用linq就可以方便的做查詢,做篩選排序了
還是上例中的xml,我們要讀取root的所有字節(jié)點(diǎn),并打印出來(lái),如下代碼:
復(fù)制代碼 代碼如下:
class Program
{
static void Main(string[] args)
{
var xDoc = new XDocument(new XElement( "root",
new XElement("dog",
new XText("dog said black is a beautify color"),
new XAttribute("color", "black")),
new XElement("cat"),
new XElement("pig", "pig is great")));
//xDoc輸出xml的encoding是系統(tǒng)默認(rèn)編碼,對(duì)于簡(jiǎn)體中文操作系統(tǒng)是gb2312
//默認(rèn)是縮進(jìn)格式化的xml,而無(wú)須格式化設(shè)置
xDoc.Save(Console.Out);
Console.WriteLine();
var query = from item in xDoc.Element( "root").Elements()
select new
{
TypeName = item.Name,
Saying = item.Value,
Color = item.Attribute("color") == null?(string)null:item.Attribute("color").Value
};
foreach (var item in query)
{
Console.WriteLine("{0} 's color is {1},{0} said {2}",item.TypeName,item.Color??"Unknown",item.Saying??"nothing");
}
Console.Read();
}
}
3. Linq to xml簡(jiǎn)單的應(yīng)用
應(yīng)用需求: 讀取博客園的rss,然后在頁(yè)面上輸出最新的10篇博客信息
實(shí)現(xiàn)要點(diǎn): 通過(guò)XDocument的Load靜態(tài)方法載入Xml,通過(guò)linq查詢最新10條數(shù)據(jù)
代碼如下:
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
//實(shí)際應(yīng)用,通過(guò)讀取博客園的RSS生成Html代碼顯示最新的博客列表
//使用XDocument的Load靜態(tài)方法載入Xml
var rssXDoc = XDocument.Load("http://www.dhdzp.com");
//使用linq to xml查詢前10條新博客
var queryBlogs = (from blog in rssXDoc.Descendants("item")
select new
{
Title = blog.Element("title").Value,
Url = blog.Element("link").Value,
PostTime = DateTime.Parse(blog.Element("pubDate").Value)
}).Take(20);
repeaterBlogs.DataSource = queryBlogs;
repeaterBlogs.DataBind();
base.OnLoad(e);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Linq to Xml 實(shí)例</title>
</head>
<body>
<ol>
<asp:Repeater ID="repeaterBlogs" EnableViewState="false" runat="server">
<ItemTemplate>
<li><span style="float: right">
<%#Eval("PostTime") %></span><a href="<%#Eval("Url") %>"><%#Eval("Title") %></a></li>
</ItemTemplate>
</asp:Repeater>
</ol>
</body>
</html>
C#的發(fā)展讓讀寫Xml越來(lái)越簡(jiǎn)單了。
相關(guān)文章
asp.net+xml+flash實(shí)現(xiàn)的圖片展示效果示例
這篇文章主要介紹了asp.net+xml+flash實(shí)現(xiàn)的圖片展示效果的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了圖片展示效果的相關(guān)操作步驟與flash與xml調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-08-08
asp.net中Null在從數(shù)據(jù)庫(kù)讀取的時(shí)候的一點(diǎn)點(diǎn)小技巧
我們先看下面的一段代碼,這段代碼其實(shí)很平常,也是我們平時(shí)做項(xiàng)目很常用的一段2012-04-04
ASP.NET 2.0服務(wù)器控件開發(fā)之復(fù)雜屬性
ASP.NET 2.0服務(wù)器控件開發(fā)之復(fù)雜屬性...2006-09-09
.Net Core配置與自動(dòng)更新的實(shí)現(xiàn)方法
下面小編就為大家分享一篇.Net Core配置與自動(dòng)更新的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
.Net?Api?中使用Elasticsearch存儲(chǔ)文檔的方法
Elasticsearch 是一個(gè)分布式、高擴(kuò)展、高實(shí)時(shí)的搜索與數(shù)據(jù)分析引擎,在C# 的環(huán)境中,有一個(gè)Es的官方拓展包Nest,可以讓我們方便快捷的使用上Es數(shù)據(jù)庫(kù),本文重點(diǎn)給大家介紹.Net?Api?中使用Elasticsearch存儲(chǔ)文檔的方法,感興趣的朋友一起看看吧2022-01-01
WPF使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate
本文詳細(xì)講解了WPF使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
用WebClient.UploadData方法上載文件數(shù)據(jù)的方法
用WebClient.UploadData方法上載文件數(shù)據(jù)的方法...2007-04-04

