C# Webbrowser控件:获取元素值并将其存储到变量中
Winform:Web浏览器控件 web浏览器在html表中显示以下内容C# Webbrowser控件:获取元素值并将其存储到变量中,c#,winforms,webbrowser-control,C#,Winforms,Webbrowser Control,Winform:Web浏览器控件 web浏览器在html表中显示以下内容 [Element] [Value] Name John Smith Email jsmith@hotmail.com 对于上面的示例,html代码可能如下所示 <table> <tbody> <tr> <td><label class="label">Name</label></t
[Element] [Value]
Name John Smith
Email jsmith@hotmail.com
对于上面的示例,html代码可能如下所示
<table>
<tbody>
<tr>
<td><label class="label">Name</label></td>
<td class="normaltext">John Smith</td>
</tr>
<tr> <td><label class="label">Email</label></td>
<td><span class="normaltext">jsmith@hotmail.com</span></td>
</tr>
</tr>
</tbody>
</table>
名称
约翰·史密斯
电子邮件
jsmith@hotmail.com
我想获取元素值,即标签右侧的值。
最好的方法是什么?
(我可以使用DOM吗,或者我需要用正则表达式分阶段处理html代码吗?)
实现这一点的方法不止一种 例如,这对你有用吗
using System.Windows.Forms;
namespace TestWebBrowser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.DocumentText = @"<html><body><table>
<tbody>
<tr>
<td><label class=""label"">Name</label></td>
<td class=""normaltext"">John Smith</td>
</tr>
<tr> <td><label class=""label"">Email</label></td>
<td><span class=""normaltext"" id=""e1"">jsmith@hotmail.com</span></td>
</tr>
</tr>
</tbody>
</table>
</body>
</html>";
}
private void button1_Click(object sender, System.EventArgs e)
{
HtmlElement e1 = webBrowser1.Document.GetElementById("e1");
MessageBox.Show(e1.InnerText);
}
}
}
使用System.Windows.Forms;
命名空间TestWebBrowser
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
webBrowser1.DocumentText=@“
名称
约翰·史密斯
电子邮件
jsmith@hotmail.com
";
}
私有无效按钮1\u单击(对象发送者,System.EventArgs e)
{
HtmlElement e1=webBrowser1.Document.GetElementById(“e1”);
MessageBox.Show(e1.InnerText);
}
}
}
如何检查元素是否首先存在?因为它抛出了一个nullrefexep错误。