读取XML新闻

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Xml;
None.gif
None.gif
None.gif
None.gif
public   class  ReadXmlNews
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected string Url = "";
InBlock.gif    
protected int amount;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
构造函数#region 构造函数
InBlock.gif    
public ReadXmlNews()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Url 
= "";
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
封装字段#region 封装字段
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 一共的记录条数
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public int AmountLines
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn amount; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ amount = value; }
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 新闻地址
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public string NewsUrl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn Url; }
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
查询出XML文件中所有的新闻信息#region 查询出XML文件中所有的新闻信息
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// XML文件中所有的新闻信息
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <returns>XML文件中所有的新闻信息</returns>

InBlock.gif    public DataTable ProcessXML(string Str_url)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string lastid = "";
InBlock.gif        
//添加DataTable,并且设置新的列
InBlock.gif
        DataTable result = new DataTable();
InBlock.gif        result.Columns.Add(
"pubDate"typeof(string));
InBlock.gif        result.Columns.Add(
"title"typeof(string));
InBlock.gif        result.Columns.Add(
"description"typeof(string));
InBlock.gif        result.Columns.Add(
"link"typeof(string));
InBlock.gif        DataRow DR 
= result.NewRow();
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlTextReader objXMLReader 
= new XmlTextReader(Str_url);
InBlock.gif            
while (objXMLReader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlNodeType objNodeType 
= objXMLReader.NodeType;
InBlock.gif                
if (objNodeType.ToString() == "Element")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    lastid 
= objXMLReader.Name;
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (objNodeType.ToString() == "Text")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (lastid == "title")
InBlock.gif                        DR[lastid] 
= objXMLReader.Value;
InBlock.gif                    
if (lastid == "link")
InBlock.gif                        DR[lastid] 
= objXMLReader.Value;
InBlock.gif                    
if (lastid == "description")
InBlock.gif                        DR[lastid] 
= objXMLReader.Value;
InBlock.gif                    
if (lastid == "pubDate")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        DateTime dt 
= Convert.ToDateTime(objXMLReader.Value);
InBlock.gif                        
string ch = dt.ToShortDateString();
InBlock.gif                        DR[lastid] 
= ch;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
// DR[lastid] = objXMLReader.Value;
ExpandedSubBlockEnd.gif
                }

InBlock.gif
InBlock.gif                
if (objXMLReader.Name == "item" && objXMLReader.NodeType.ToString() == "EndElement")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    result.Rows.Add(DR);
InBlock.gif                    DR 
= result.NewRow();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            objXMLReader.Close();
InBlock.gif            
int num = result.Rows.Count;
InBlock.gif            amount 
= num;
InBlock.gif            Url 
= Str_url;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return result;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/domkey0303/archive/2006/10/16/530490.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值