0

I am creating a webpage which will retreive some form data from lots of other pages. For instance a hidden form field:

<input type="hidden" id="thefield" value="thegoods" />

Now this field will always have the same ID, but is sometimes written with double quotes ("), sometimes single (') and sometimes none. So this makes it more difficult to just do an "indexof" to retreive the required data.

Is there any way in c#.net to retreive the value of form elements in a jquery kind of way.

So I can just specify the elements ID, and it's value is returned.

Any help appreciated.

Thanks

Grant Unwin

0

2 Answers 2

7

The easiest way is probably using the HtmlAgilityPack.

Example:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load("test.html");

HtmlNode node = doc.DocumentNode.SelectSingleNode("//input[@id='thefield']");
var value = node.Attributes["value"].Value; //"thegoods"

Edit:

to load an HTML page from the web instead of a local file use HtmlWeb :

HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument doc = htmlWeb.Load("http://stackoverflow.com");
...
Sign up to request clarification or add additional context in comments.

4 Comments

How do I include the DLL into my project? I've never used a third-party dll before.
@Grant: Also the easiest way to get HtmlAgilityPack into your project is using the NuGet packet manager - you can install NuGet as a VS extension and go from there. Alternatively you can just download HtmlAgility from codeplex and reference the dll directly
do you think this will work with a live site. Or would I need to use a StreamReader?
@Grant - you mean loading from a URL? Updated to add an example for that
0

I recommend HtmlAgilityPack, but it's based on XPath rather than JQuery. Fizzler has a syntax much closer to JQuery, but is not nearly as mature.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.