开发者

Processing HTML document with C#

I have a few hundred static HTML files that need to be processed.

They contain links like this

 <a href="http://www.mysite.com/">Link</a>

I need to add ?ref=self to 开发者_如何学Pythonany url that begins with http://www.mysite.com and becomes

<a href="http://www.mysite.com/?ref=self">Link</a>

however, I do not know whether it's going to be http://www.mysite.com or http://www.mysite.com/ also it could be linked to a sub directory.

What's the most efficient way to do this? in C#


What's the most efficient way to do this? in C#

  1. Look for the string http://www.mysite.com.
  2. If it doesn't exist, go to 7.
  3. Look for the next ".
  4. If it doesn't exist, error.
  5. Insert ?ref=self before the ".
  6. Go to 1.
  7. Return.

This can be accomplished with the following regular expression substitution:

s#http://www.mysite.com[^"]*#&?ref=self#g

A nicer (more expressive) way would be to use an HTML parser and XPath.


Parsing HTML can be tricky as HTML often contains poorly formed tags and attributes. I suggest looking into an existing HTML parsing library to do your heavy lifting, or, using XSLT to transform valid (x)HTML to your desired output.

This question What is the best way to parse html in C#? has some good links to HTML parsing libraries for C#.


You could use Page.Request.UrlReferrer to detect where the request came from.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜