在VB.Net中使用MSHTML解析HTML
想知道是否有人可以给我一些指导。我在这上面花了相当多的时间,但似乎没有取得任何进展:
我有一个隐藏字段,我正试图从 VB.Net 中的 HTML 文档中解析它。我在 WPF 应用程序中使用 System.Windows.Controls.WebBrowser 控件并处理 LoadCompleted 事件。在 LoadCompleted 事件处理程序中,我执行如下操作:
Dim htmlDocument As mshtml.IHTMLDocument2 = Me.WebBrowser.Document
Dim allElements As mshtml.IHTMLElementCollection = htmlDocument.body.all
Dim hiddenField As mshtml.IHTMLInputElement = allElements.tags("hidField")
我尝试访问的隐藏字段在我的 .aspx 文件中声明如下:
<asp:HiddenField runat="server" ID="hidField"/>
问题是这个 allElements.tags("hidField")
返回 null。我对 mshtml 库做错了什么吗?我对此没有太多经验,并且认为我需要做这样的事情来找到我的隐藏字段元素。如果您需要更多信息,请告诉我。感谢您提前的帮助。
编辑
对于任何感兴趣的人来说,这是我的最终工作解决方案:
Dim htmlDocument As mshtml.IHTMLDocument2 = Me.WebBrowser.Document
Dim allElements As mshtml.IHTMLElementCollection = htmlDocument.body.all
Dim allInputs As mshtml.IHTMLElementCollection = allElements.tags("input")
For Each element As mshtml.IHTMLInputElement In allInputs
If element.type = "hidden" And element.name.Contains("hidField") Then
MessageBox.Show(element.value)
End If
Next
Was wondering if someone could give me some direction on this. I've spent a decent amount of time on it and don't seem to be getting anywhere:
I have a hidden field that I'm trying to parse out of an HTML document in VB.Net. I'm using a System.Windows.Controls.WebBrowser control in a WPF application and handling the LoadCompleted event. Inside the LoadCompleted event handler I do something like this:
Dim htmlDocument As mshtml.IHTMLDocument2 = Me.WebBrowser.Document
Dim allElements As mshtml.IHTMLElementCollection = htmlDocument.body.all
Dim hiddenField As mshtml.IHTMLInputElement = allElements.tags("hidField")
The hidden field that I'm trying to access is declared in my .aspx file as such:
<asp:HiddenField runat="server" ID="hidField"/>
The problem is that this allElements.tags("hidField")
is returning null. Am I doing something wrong with the mshtml library? I don't have much experience with it and gathered that I needed to do something like this to find my hidden field element. Let me know if you need more info. Thanks for the help in advance.
EDIT
Here's is my final working solution for anyone interested:
Dim htmlDocument As mshtml.IHTMLDocument2 = Me.WebBrowser.Document
Dim allElements As mshtml.IHTMLElementCollection = htmlDocument.body.all
Dim allInputs As mshtml.IHTMLElementCollection = allElements.tags("input")
For Each element As mshtml.IHTMLInputElement In allInputs
If element.type = "hidden" And element.name.Contains("hidField") Then
MessageBox.Show(element.value)
End If
Next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要查找呈现的标签,而不是服务器端值。
这将被渲染为
,因此您需要使用
allElements.tags("input")
,然后找到特定的隐藏一。id
属性可能不会最终成为hidField
- 这取决于它所在的容器控件以及嵌套级别。我建议使用 HTML Agilty Pack 来解析 HTML 并查找元素 - 它应该比MSHTML。
You need to look for the rendered tag, not the serverside value.
This will be rendered as an
<input type="hidden">
, so you need to useallElements.tags("input")
, then find the specific hidden one. Theid
attribute may not end up ashidField
- it depends on what container controls it is in and in what nesting level.I suggest using the HTML Agilty Pack to parse the HTML and find the element instead - it should be easier to use than MSHTML.
自从我使用 MSHTML 以来已经有一段时间了,但如果我没记错的话,你需要使用类似的东西
htmlDocument.getElementById('hidField')
Its been a while since I used MSHTML, but if I remember correctly you need to use something like
htmlDocument.getElementById('hidField')