2013年9月11日 星期三

具有xmlns的NameSpace設定的XML讀取及使用Linq的where條件做查詢


            //****************************************************************************************************
            Response.Write("不帶Namespace的XML<br>");
            XmlDocument xd1 = new XmlDocument();
            xd1.Load("http://myblog.com/rss");
            XmlNodeList rssItems1 = xd1.SelectNodes("//item");
            Response.Write("<ul>");
            foreach (XmlNode node in rssItems1)
            {
                Response.Write("<li>");
                Response.Write("    <a href=" + node["link"].InnerText + ">" + node["title"].InnerText + "</a>");
                Response.Write("</li>");
            }
            Response.Write("</ul>");
            //****************************************************************************************************
            //XML具有xmlns的NameSpace設定時,直接用SelectNodes或SelectSingleNode會選不到,要加上Namespace宣告
            Response.Write("有帶Namespace的XML<br>");
            XmlDocument xd2 = new XmlDocument();
            xd2.Load("http://www.cnblogs.com/rss");
            XmlNamespaceManager nsManager = new XmlNamespaceManager(xd2.NameTable);
            nsManager.AddNamespace("atom", "http://www.w3.org/2005/Atom");  //要加上這個Namespace宣告 (可多筆)
            //兩條斜線是相對路徑的去選
            //XmlNodeList rssItems2 = xd2.SelectNodes("//atom:feed/atom:entry", nsManager);
            //一條斜線是從根路徑開始去選
            //XmlNodeList rssItems2 = xd2.SelectNodes("/atom:feed/atom:entry", nsManager);
            //沒有斜線是從目前的Node之下去選
            //XmlNodeList rssItems2 = xd2.SelectNodes("atom:feed/atom:entry", nsManager);
            //直接指定子節點去選
            XmlNodeList rssItems2 = xd2.SelectNodes("//atom:entry", nsManager);
            Response.Write("<ul>");
            foreach (XmlNode node in rssItems2)
            {
                string title = node["title"].InnerText;                     //抓節點
                string author = node["author"]["name"].InnerText;           //抓子節點
                string link = node["link"].Attributes["href"].InnerText;    //抓節點中的屬性值
                Response.Write("<li>");
                Response.Write("    <a href=" + link + ">" + title + " - (作者:" + author + ")</a>");
                Response.Write("</li>");
            }
            Response.Write("</ul>");
            //****************************************************************************************************
            Response.Write("使用Linq的where條件做查詢<br>");
            //採用抓檔案的方式
            using (StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\atom.xml"))
            {
                XmlDocument xd = new XmlDocument();
                xd.Load(sr);
                XmlNamespaceManager nsMgr = new XmlNamespaceManager(xd.NameTable);
                nsMgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
                var query = from n in xd.SelectNodes("//atom:entry", nsMgr).Cast<XmlNode>()
                            where n.SelectSingleNode("atom:author/atom:name", nsMgr).InnerText.ToLower().IndexOf("a") > -1
                            select n;
                Response.Write("<ul>");
                foreach (XmlNode node in query)
                {
                    string title = node["title"].InnerText;                     //抓節點
                    string author = node["author"]["name"].InnerText;           //抓子節點
                    string link = node["link"].Attributes["href"].InnerText;    //抓節點中的屬性值
                    Response.Write("<li>");
                    Response.Write("    <a href=" + link + ">" + title + " - (作者:" + author + ")</a>");
                    Response.Write("</li>");
                }
                Response.Write("</ul>");
            }

沒有留言:

張貼留言