【【DataTable每次增加为什么会覆盖之前的数据?求助!!!】】
相关代码如下:
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
DataColumn c1 = new DataColumn("订单编号", typeof(string));
table.Columns.Add(c1);
DataColumn c2 = new DataColumn("买家昵称", typeof(string));
table.Columns.Add(c2);
DataColumn c3 = new DataColumn("卖家昵称", typeof(string));
table.Columns.Add(c3);
DataColumn c4 = new DataColumn("订单状态", typeof(string));
table.Columns.Add(c4);
DataColumn c5 = new DataColumn("商品标题", typeof(string));
table.Columns.Add(c5);
DataColumn c6 = new DataColumn("价格", typeof(string));
table.Columns.Add(c6);
ViewState["dt"] = table;
}
按钮单击部分代码:
string[,] trades = null;
trades = new string[40, 7];
int i = 0;
foreach (XmlNode node in nodeLst)
{
// Response.Write(node.SelectSingleNode("iid").InnerText);
trades[i, 0] = node.SelectSingleNode("iid").InnerText;
trades[i, 1] = node.SelectSingleNode("title").InnerText;
trades[i, 2] = node.SelectSingleNode("pic_url").InnerText;
trades[i, 3] = node.SelectSingleNode("price").InnerText;
trades[i, 4] = node.SelectSingleNode("click_url").InnerText;
trades[i, 5] = node.SelectSingleNode("nick").InnerText;
i++;
}
int c = trades.GetLength(0);
DataTable mydt = (DataTable)ViewState["dt"];
for (i = 0; i < c; i++)
{
DataRow r1 = mydt.NewRow();
r1["订单编号"] = trades[i, 0];
r1["买家昵称"] = trades[i, 1];
r1["卖家昵称"] = trades[i, 2];
r1["订单状态"] = trades[i, 3];
r1["商品标题"] = trades[i, 4];
r1["价格"] = trades[i, 5];
mydt.Rows.Add(r1);
mydt.AcceptChanges();
}
this.GridView1.DataSource = mydt;
GridView1.DataBind();
按钮的前面部分是获取的一个XML,我想获取到XML后再加入到这个GridView里,但是现在的情况是覆盖了之前的数据。
不知道如何实现不覆盖,或者我这个代码有哪些问题,请赐教,谢谢!
------解决方案--------------------gx
------解决方案--------------------接分中。。
------解决方案--------------------
if(!isPostBack)
{
}
使用mshtml和正则解析html
或XPath
string tag = @"(?:[\w-:]+)";
string attribute = @"(?:[\w-:]+)(?:=(?:[^\s\>\<]*|\""[\s\S]*?\""|\'[\s\S]*?\'))?";
string name = @"(?:[\w-:]+)";
string xmlDirective = @"(?:\<!" +name + @"(?:\s+" +argument + @")*\s*\>)";
string xmlCData = @"(?:\<!\[CDATA\[(?:[\s\S]*?)\]\]\>)";
参考
------解决方案--------------------
C# code
using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Text.RegularExpressions;
5using MIL.Html;
6
7namespace Yuanso.Sitework.Crawler
8{
9 public class HtmlUtil
10 {
11 /**//// <summary>
12 ///