C#读取网站的数据
2024-07-21 02:26:23
供稿:网友
菜鸟学堂:
以下是引用片段:form1.cs
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.xml;
namespace hellojoysou
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.datagrid visitors;
private system.windows.forms.button getxml;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.visitors = new system.windows.forms.datagrid();
this.getxml = new system.windows.forms.button();
((system.componentmodel.isupportinitialize)(this.visitors)).begininit();
this.suspendlayout();
//
// visitors
//
this.visitors.captiontext = "visitors";
this.visitors.datamember = "";
this.visitors.headerforecolor = system.drawing.systemcolors.controltext;
this.visitors.location = new system.drawing.point(8, 8);
this.visitors.name = "visitors";
this.visitors.preferredcolumnwidth = 100;
this.visitors.size = new system.drawing.size(552, 184);
this.visitors.tabindex = 0;
this.visitors.navigate += new system.windows.forms.navigateeventhandler(this.visitors_navigate);
//
// getxml
//
this.getxml.location = new system.drawing.point(32, 240);
this.getxml.name = "getxml";
this.getxml.size = new system.drawing.size(64, 16);
this.getxml.tabindex = 1;
this.getxml.text = "getxml";
this.getxml.click += new system.eventhandler(this.getxml_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(568, 278);
this.controls.add(this.getxml);
this.controls.add(this.visitors);
this.maximizebox = false;
this.name = "form1";
this.text = "form1";
this.load += new system.eventhandler(this.form1_load);
((system.componentmodel.isupportinitialize)(this.visitors)).endinit();
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void form1_load(object sender, system.eventargs e)
{
}
private void getxml_click(object sender, system.eventargs e)
{
xmldocument xml=new xmldocument(); //关键用默认构造函数创建xmldocument
xml.load("http://www.joysou.com/system/nothing.asp"); //加载xml文件
xmlnode root=xml.documentelement.selectnodes("/joysou").item(0);
xmlnodelist visitor=root.selectnodes("info/visitors/visitor");
datatable dt = new datatable();
dt.columns.add(new datacolumn("name", typeof(string)));
dt.columns.add(new datacolumn("ip", typeof(string)));
dt.columns.add(new datacolumn("address", typeof(string)));
dt.columns.add(new datacolumn("os", typeof(string)));
dt.columns.add(new datacolumn("soft", typeof(string)));
for (int i=0;i<visitor.count;i++)
{
xmlattributecollection tnode=visitor.item(i).attributes;
dt.rows.add(new string[] {tnode["name"].value,tnode["ip"].value,tnode["address"].value,tnode["os"].value,tnode["soft"].value});
}
visitors.datasource = dt;
}
private void visitors_navigate(object sender, system.windows.forms.navigateeventargs ne)
{
}
}
}