程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#讀取網站的數據

C#讀取網站的數據

編輯:關於C語言
數據以下是引用片段: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)
{

}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved