using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Configuration; using System.Collections; using System.Xml; public class ReadXmlNews { protected string Url = ""; protected int amount; 构造函数#region 构造函数 public ReadXmlNews() { Url = ""; } #endregion 封装字段#region 封装字段 /**//// <summary> /// 一共的记录条数 /// </summary> public int AmountLines { get { return amount; } set { amount = value; } } /**//// <summary> /// 新闻地址 /// </summary> public string NewsUrl { get { return Url; } } #endregion 查询出XML文件中所有的新闻信息#region 查询出XML文件中所有的新闻信息 /**//// <summary> /// XML文件中所有的新闻信息 /// </summary> /// <returns>XML文件中所有的新闻信息</returns> public DataTable ProcessXML(string Str_url) { string lastid = ""; //添加DataTable,并且设置新的列 DataTable result = new DataTable(); result.Columns.Add("pubDate", typeof(string)); result.Columns.Add("title", typeof(string)); result.Columns.Add("description", typeof(string)); result.Columns.Add("link", typeof(string)); DataRow DR = result.NewRow(); try { XmlTextReader objXMLReader = new XmlTextReader(Str_url); while (objXMLReader.Read()) { XmlNodeType objNodeType = objXMLReader.NodeType; if (objNodeType.ToString() == "Element") { lastid = objXMLReader.Name; } if (objNodeType.ToString() == "Text") { if (lastid == "title") DR[lastid] = objXMLReader.Value; if (lastid == "link") DR[lastid] = objXMLReader.Value; if (lastid == "description") DR[lastid] = objXMLReader.Value; if (lastid == "pubDate") { DateTime dt = Convert.ToDateTime(objXMLReader.Value); string ch = dt.ToShortDateString(); DR[lastid] = ch; } // DR[lastid] = objXMLReader.Value; } if (objXMLReader.Name == "item" && objXMLReader.NodeType.ToString() == "EndElement") { result.Rows.Add(DR); DR = result.NewRow(); } } objXMLReader.Close(); int num = result.Rows.Count; amount = num; Url = Str_url; } catch (Exception err) { } return result; } #endregion} 转载于:https://www.cnblogs.com/domkey0303/archive/2006/10/16/530490.html