当前位置:首页 > C#如何把html中的相对路径变成绝对路径

C#如何把html中的相对路径变成绝对路径

点击次数:1695  更新日期:2010-12-24
\n

private static string ConvertToAbsoluteUrls (string html, Uri relativeLocation) {
IHTMLDocument2 doc = new HTMLDocumentClass ();
doc.write (new object [] { html });
doc.close ();


\n

foreach (IHTMLAnchorElement anchor in doc.links) {
IHTMLElement element = (IHTMLElement)anchor;
string href = (string)element.getAttribute (“href”, 2);
if (href != null) {
Uri addr = new Uri (relativeLocation, href);
anchor.href = addr.AbsoluteUri;
}
}


\n

foreach (IHTMLImgElement image in doc.images) {
IHTMLElement element = (IHTMLElement)image;
string src = (string)element.getAttribute (“src”, 2);
if (src != null) {
Uri addr = new Uri (relativeLocation, src);
image.src = addr.AbsoluteUri;
}
}


\n

string ret = doc.body.innerHTML;


\n

return ret;
}


\n

来源:网络

\n