佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 解析linq to xml操作XML的示例分析

解析linq to xml操作XML的示例分析

熱門標(biāo)簽:服務(wù)器配置 銀行業(yè)務(wù) 團(tuán)購網(wǎng)站 阿里云 Mysql連接數(shù)設(shè)置 科大訊飛語音識別系統(tǒng) 電子圍欄 Linux服務(wù)器
.Net中的System.Xml.Linq命名空間提供了linq to xml的支持。這個命名空間中的XDocument,XElement以及XText,XAttribute提供了讀寫xml文檔的關(guān)鍵方法。
1. 使用linq to xml寫xml:
使用XDocument的構(gòu)造函數(shù)可以構(gòu)造一個Xml文檔對象;使用XElement對象可以構(gòu)造一個xml節(jié)點元素,使用XAttribute構(gòu)造函數(shù)可以構(gòu)造元素的屬性;使用XText構(gòu)造函數(shù)可以構(gòu)造節(jié)點內(nè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)編碼,對于簡體中文操作系統(tǒng)是gb2312
        //默認(rèn)是縮進(jìn)格式化的xml,而無須格式化設(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是從集合中查詢對象,在linq to xml中的集合是通過XElement的Elements(),Elements(string name),以及Descendants、DescendantsAndSelf、Ancestors、AncestorsAndSelf的幾個重載方法中獲得。
獲得XElement集合之后,可以通過XElement的Attribute(string name)方法獲得元素的屬性值,可以通過XElement的Value屬性獲得節(jié)點的文本值;使用linq就可以方便的做查詢,做篩選排序了
還是上例中的xml,我們要讀取root的所有字節(jié)點,并打印出來,如下代碼:
復(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)編碼,對于簡體中文操作系統(tǒng)是gb2312
        //默認(rèn)是縮進(jìn)格式化的xml,而無須格式化設(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簡單的應(yīng)用
應(yīng)用需求: 讀取博客園的rss,然后在頁面上輸出最新的10篇博客信息
實現(xiàn)要點: 通過XDocument的Load靜態(tài)方法載入Xml,通過linq查詢最新10條數(shù)據(jù)
代碼如下:
復(fù)制代碼 代碼如下:

%@ Page Language="C#" AutoEventWireup="true" %>
script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        //實際應(yīng)用,通過讀取博客園的RSS生成Html代碼顯示最新的博客列表
        //使用XDocument的Load靜態(tài)方法載入Xml
        var rssXDoc = XDocument.Load("https://www.jb51.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越來越簡單了。
您可能感興趣的文章:
  • C# Linq讀取XML文件的實例
  • c#使用linq技術(shù)創(chuàng)建xml文件的小例子
  • C# LINQ to XML應(yīng)用介紹
  • Linq to XML 用一句話讀出RSS文章列表代碼
  • asp.net Linq to Xml學(xué)習(xí)筆記
  • LINQ to XML的編程基礎(chǔ)
  • asp.net Linq To Xml上手Descendants、Elements遍歷節(jié)點
  • 使用linq to xml修改app.config示例(linq讀取xml)

標(biāo)簽:蚌埠 棗莊 衡水 江蘇 衢州 大理 萍鄉(xiāng) 廣元

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解析linq to xml操作XML的示例分析》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    平利县| 盐边县| 涿鹿县| 大悟县| 天门市| 砀山县| 巴塘县| 芜湖县| 澄城县| 奎屯市| 安丘市| 青海省| 司法| 明溪县| 赤壁市| 罗平县| 保亭| 扶风县| 广河县| 高平市| 苗栗县| 大埔区| 保山市| 新乡县| 繁昌县| 阜平县| 闻喜县| 安国市| 揭西县| 玉环县| 太谷县| 察隅县| 来凤县| 外汇| 昭觉县| 朔州市| 吉水县| 望城县| 米林县| 伽师县| 石柱|