MaxRecursion
MaxRecursion

Reputation: 4903

Process HTML Markup in C#

I want to process/manipulate some HTML markup

e.g.

<a id="flFileList_gvDoItFiles_btnContent_1" href="javascript:__doPostBack('flFileList$gvDoItFiles$ctl03$btnContent','')">Untitled.png.3154ROGG635264188946573079.png</a>

changed to

<a id="flFileList_gvDoItFiles_btnContent_1" href="javascript:__doPostBack('flFileList$gvDoItFiles$ctl03$btnContent','')">Untitled.png</a>

I want achieve this using C# string processing.

Not getting any idea for this. I have logic written convert

Untitled.png.3154ROGG635264188946573079.png to

Untitled.png

I am stuck in how do I identify and replace th string in markup?

String.Split()??

Upvotes: 0

Views: 912

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236318

I suggest you to use HtmlAgilityPack for parsing HTML. You can easily get a element by it's id, and then replace it's inner text:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html_string);
string xpath = "//a[@id='flFileList_gvDoItFiles_btnContent_1']";
var a = doc.DocumentNode.SelectSingleNode(xpath);
a.InnerHtml = ConvertValue(a.InnerHtml); // call your logic for converting value
string result = a.OuterHtml;

Upvotes: 2

Related Questions