首页 > 编程 > HTML > 正文

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

2024-08-26 00:15:37
字体:
来源:转载
供稿:网友

private static string converttoabsoluteurls (string html, uri relativelocation) {
    ihtmldocument2 doc = new htmldocumentclass ();
    doc.write (new object [] { html });
    doc.close ();

    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;
        }
    }

    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;
        }
    }

    string ret = doc.body.innerhtml;

    return ret;
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表