且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解析HTML中的字段

更新时间:2023-02-23 17:34:56

您好!

希望问题尚未解决,并且我理解您的问题,这是我的建议:
使用HTML Agility Pack是一个好主意,它几乎类似于Microsoft XmlDocument,但支持html(即,如果html格式错误,它不会崩溃).
解决您的问题的方法可能是XPath (这是一个非常不错的教程)-您只需使用一个XPath表达式可以枚举您所有具有id的元素:

Hello!

In hope the question is not already solved and I understand your problem, here my recommendation:
Using the HTML Agility Pack is a good idea, it is almost like the Microsoft XmlDocument but supports html (i.e. it does not crash if the html is malformed).
The solution to your problem might be XPath (here is a really good tutorial) - with a single XPath-expression you can enumerate through all your elements which have an id:

HtmlDocument hd = new HtmlDocument();
//Load your stuff with hd.Load(Stream, Encoding)

foreach (var node in hd.DocumentNode.SelectNodes("//input[@id]"))
  //Select all input-Elements which have an id-attribute
{
  //Here you can do what you want with your node
}