会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
domkey0303
博客园
首页
新随笔
联系
订阅
管理
公告
读取XML新闻
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
}
posted on
2006-10-16 17:07
Robin Young
阅读(
349
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
Powered by:
博客园
Copyright © 2024 Robin Young
Powered by .NET 8.0 on Kubernetes