解析linq to xml操作XML的示例分析
更新時間:2013年05月18日 17:19:50 作者:
本篇文章是對linq to xml操作XML的示例進行了詳細的分析介紹,需要的朋友參考下
.Net中的System.Xml.Linq命名空間提供了linq to xml的支持。這個命名空間中的XDocument,XElement以及XText,XAttribute提供了讀寫xml文檔的關鍵方法。
1. 使用linq to xml寫xml:
使用XDocument的構造函數可以構造一個Xml文檔對象;使用XElement對象可以構造一個xml節(jié)點元素,使用XAttribute構造函數可以構造元素的屬性;使用XText構造函數可以構造節(jié)點內的文本。
如下實例代碼:
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是系統默認編碼,對于簡體中文操作系統是gb2312
//默認是縮進格式化的xml,而無須格式化設置
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是從集合中查詢對象,在linq to xml中的集合是通過XElement的Elements(),Elements(string name),以及Descendants、DescendantsAndSelf、Ancestors、AncestorsAndSelf的幾個重載方法中獲得。
獲得XElement集合之后,可以通過XElement的Attribute(string name)方法獲得元素的屬性值,可以通過XElement的Value屬性獲得節(jié)點的文本值;使用linq就可以方便的做查詢,做篩選排序了
還是上例中的xml,我們要讀取root的所有字節(jié)點,并打印出來,如下代碼:
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是系統默認編碼,對于簡體中文操作系統是gb2312
//默認是縮進格式化的xml,而無須格式化設置
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簡單的應用
應用需求: 讀取博客園的rss,然后在頁面上輸出最新的10篇博客信息
實現要點: 通過XDocument的Load靜態(tài)方法載入Xml,通過linq查詢最新10條數據
代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
//實際應用,通過讀取博客園的RSS生成Html代碼顯示最新的博客列表
//使用XDocument的Load靜態(tài)方法載入Xml
var rssXDoc = XDocument.Load("http://m.fzitv.net");
//使用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 實例</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越來越簡單了。
1. 使用linq to xml寫xml:
使用XDocument的構造函數可以構造一個Xml文檔對象;使用XElement對象可以構造一個xml節(jié)點元素,使用XAttribute構造函數可以構造元素的屬性;使用XText構造函數可以構造節(jié)點內的文本。
如下實例代碼:
復制代碼 代碼如下:
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是系統默認編碼,對于簡體中文操作系統是gb2312
//默認是縮進格式化的xml,而無須格式化設置
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是從集合中查詢對象,在linq to xml中的集合是通過XElement的Elements(),Elements(string name),以及Descendants、DescendantsAndSelf、Ancestors、AncestorsAndSelf的幾個重載方法中獲得。
獲得XElement集合之后,可以通過XElement的Attribute(string name)方法獲得元素的屬性值,可以通過XElement的Value屬性獲得節(jié)點的文本值;使用linq就可以方便的做查詢,做篩選排序了
還是上例中的xml,我們要讀取root的所有字節(jié)點,并打印出來,如下代碼:
復制代碼 代碼如下:
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是系統默認編碼,對于簡體中文操作系統是gb2312
//默認是縮進格式化的xml,而無須格式化設置
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簡單的應用
應用需求: 讀取博客園的rss,然后在頁面上輸出最新的10篇博客信息
實現要點: 通過XDocument的Load靜態(tài)方法載入Xml,通過linq查詢最新10條數據
代碼如下:
復制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
//實際應用,通過讀取博客園的RSS生成Html代碼顯示最新的博客列表
//使用XDocument的Load靜態(tài)方法載入Xml
var rssXDoc = XDocument.Load("http://m.fzitv.net");
//使用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 實例</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越來越簡單了。
相關文章
.Net?Api?中使用Elasticsearch存儲文檔的方法
Elasticsearch 是一個分布式、高擴展、高實時的搜索與數據分析引擎,在C# 的環(huán)境中,有一個Es的官方拓展包Nest,可以讓我們方便快捷的使用上Es數據庫,本文重點給大家介紹.Net?Api?中使用Elasticsearch存儲文檔的方法,感興趣的朋友一起看看吧2022-01-01
WPF使用代碼創(chuàng)建數據模板DataTemplate
本文詳細講解了WPF使用代碼創(chuàng)建數據模板DataTemplate的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
用WebClient.UploadData方法上載文件數據的方法
用WebClient.UploadData方法上載文件數據的方法...2007-04-04

