标签:XML 读取 新闻 objXMLReader result using lastid DR string
原文链接:http://www.cnblogs.com/domkey0303/archive/2006/10/16/530490.html 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
标签:XML,读取,新闻,objXMLReader,result,using,lastid,DR,string 来源: https://blog.csdn.net/weixin_30371875/article/details/97508309
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。