我是如何从HTML表C#获取数据的 [英] How I get data from HTML table C#
问题描述
我有html页面,其中有许多元素,其中一个是表。如何获取表中的值并将它们附加到datatable我使用html agility asp.net c#
此表是scrpit代码并且具有(\)每个项目
i have html page where there are many elements one of them is a table . How can i get the values in the table and append them to datatable i use html agility asp.net c#
this table is in scrpit code and have (\)with each item
<script type="text/javascript">
<!--
bklkl.abc.writeWidget({'cons':'bobj.crv.newpage'; style sheet properties with alot of character})
<\table cellspacing="0" cellpadding="3" rules="cols" id="page1">
<\tr>
<\th scope="col">h1</th>
<\th scope="col">h2</th>
<\th scope="col">h3 </th>
<\th scope="col"> h4</th>
<\th scope="col"> h5</th>
<\tr>
<\tr >
<\td><input type="button" value="edit" /></td>
<\td>value 1</td>
<\td>value 2 </td>
<\td>value 3</td>
<\td>value 4</td>
</tr>
<\table>
//-->
</script>
$ b $由于css和java代码而无法获取表格??
i甚至尝试
i cant get the table because of the css and java code ??
i even try
string text = Regex.Replace(infodata.InnerText, @"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>", string.Empty).Replace(@"\r\n", "").Replace(@"\", "").Replace(@"\r\", "");
但我仍然无法访问或阅读表格的CSS代码
我试过的:
这是我的试用版
< pre lang =C#> HtmlNodeCollection tables = pag1.Html .SelectNodes(& quot; // table [@ id =&#39; data&#39;]& quot;);
DataTable tb = new DataTable();
HtmlNodeCollection rows = tables [0] .SelectNodes(& quot; tr& quot;);
/ /创建列
HtmlNodeCollection cols = rows [0] .SelectNodes(& quot; th& quot;);
if(cols!= null)
{
for(int j = 0; j& lt; = cols.Count - 1; j ++)
{
tb.Columns.Add(cols [j] .InnerText);
}
}
//现在填写表格
for(int i = 0; i& lt; = rows.Count - 1; i ++)
{
var newRow = tb.NewRow();
HtmlNodeCollection cols = rows [i] .SelectNodes(& quot; td& quot;);
if(cols!= null)
{
for(int j = 0; j& lt; = cols.Count - 1; j ++)
{
newRow [j] = cols [j] .InnerText;
}
}
//将行添加到表格
tb.Rows.Add (newRow);
}
but i get still css code that i cant reach or read the table
What I have tried:
This is my trial
<pre lang="C#">HtmlNodeCollection tables = pag1.Html.SelectNodes("//table[@id='data']");
DataTable tb = new DataTable();
HtmlNodeCollection rows = tables[0].SelectNodes("tr");
// create the columns
HtmlNodeCollection cols = rows[0].SelectNodes("th");
if (cols != null)
{
for (int j = 0; j <= cols.Count - 1; j++)
{
tb.Columns.Add(cols[j].InnerText);
}
}
// Now fill the table
for (int i = 0; i <= rows.Count - 1; i++)
{
var newRow = tb.NewRow();
HtmlNodeCollection cols = rows[i].SelectNodes("td");
if (cols != null)
{
for (int j = 0; j <= cols.Count - 1; j++)
{
newRow[j] = cols[j].InnerText;
}
}
// add the row to table
tb.Rows.Add(newRow);
}
推荐答案
嗯,你的表是一个HTML构造,它不是一个控件,所以如果你发布你的页面到服务器它不会包含HTML表格中的任何内容。
您可以做的是在客户端获取数据,在您的情况下使用
Well, your table is a HTML construct it is not a control, so if you post your page to the server it will not contain anything from a HTML table.
What you can do is get the data on the client, in your case with
var lotofrawhtml =
( page1)。html();
("page1").html();
您需要了解的是ADO。 DataTable DataTables [ ^ ]与html表格不同 HTML表格标签 [ ^ ]
现在您可以做的就是为您的数据添加样式类,然后选择就像这样的 Tryit Editor v3.3 [ ^ ]
这可能是通过这种技术将其发布到某个服务 jQuery.post()| jQuery API文档 [ ^ ] ...但是你将不得不卷起袖子弄脏手;)
What you need to understand is that an ADO.DataTable DataTables[^] is not the same as a html table HTML table tag[^]
now what you can do is for instance put style classes on your data and then select that and do something with it like this Tryit Editor v3.3[^]
What that could be is posting it to some service with this technique jQuery.post() | jQuery API Documentation[^] ... but you will have to roll up your sleeves and get your hands dirty ;)
这篇关于我是如何从HTML表C#获取数据的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!