0

I need to parse a string in C# to find out code for a Particular HTML element with a given ID.

For Example:

I have a string with complete source code of a URL in it. How can I parse the string to find the inner HTML of an element of a given ID say 1,2,3...

2
  • 5
    @P.S. I'm not sure why your edit was approved, it was too minor. Try to make more substantive edits in the future. Commented May 7, 2014 at 14:34
  • 1
    @2rs2ts I did so to make question more clear in first look.I highlight the main point of question i.e. what to do. Commented May 7, 2014 at 14:49

1 Answer 1

2

Use a third party HTML parser like HTML Agility Pack.

Don't write one yourself.

Example usage:

 HtmlDocument doc = new HtmlDocument();
 doc.Load("file.htm");
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
 {
    HtmlAttribute att = link["href"];
    att.Value = FixLink(att);
 }
 doc.Save("file.htm");

Src: http://htmlagilitypack.codeplex.com/wikipage?title=Examples

Sign up to request clarification or add additional context in comments.

1 Comment

There is an error in example on the foreach line, but I can't edit it as the edit is less than six characters. The closing quote and closing square bracket are the wrong way round. Also there is a missing closing parenthesis at the end of the line.

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.