ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

读取XML新闻

2019-07-27 13:37:08  阅读:167  来源: 互联网

标签:XML 读取 新闻 objXMLReader result using lastid DR string


原文链接:http://www.cnblogs.com/domkey0303/archive/2006/10/16/530490.html None.gifusing System;
None.gifusing System.Collections.Generic;
None.gifusing System.Text;
None.gifusing System.Data;
None.gifusing System.Configuration;
None.gifusing System.Collections;
None.gifusing System.Xml;
None.gif
None.gif
None.gif
None.gifpublic class ReadXmlNews
ExpandedBlockStart.gifContractedBlock.gifdot.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.gif{ return 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.gif{ return 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

标签:XML,读取,新闻,objXMLReader,result,using,lastid,DR,string
来源: https://blog.csdn.net/weixin_30371875/article/details/97508309

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有