简体   繁体   English

使用htmlagility包替换src值

[英]Using htmlagility pack to replace src values

I'm using a CMS system for a website. 我正在为网站使用CMS系统。 My content contributors have put some very hefty images in the system and have then gone on to resize them in the cms so they are appropriate for the page or article. 我的内容贡献者在系统中放了一些非常大的图像,然后继续在cms中调整它们的大小,使它们适合于页面或文章。 When a webuser hits that page, they download the full image, even though the contributor has resized the image. 当webuser访问该页面时,即使贡献者已调整图像大小,他们也会下载完整图像。 I have found a image resizer plugin, and all I need to do is add the width and height parameters behind the image name in src. 我找到了一个图像缩放器插件,我需要做的就是在src中添加图像名称后面的width和height参数。 Doing a search it looks like I should be using the html agility pack to achieve this but can someone help me finish off my code. 进行搜索看起来我应该使用html agility pack来实现这一目标但有人可以帮我完成我的代码。 Ive figured out how to find the img tags within the content, but I dont know how to append src with the width and height. 我已经想出如何在内容中找到img标签,但我不知道如何在宽度和高度上附加src。

Old Tag 旧标签

<img src="/IMG_3556E__sq2.jpg?n=9418" id="/IMG_3556E__sq2.jpg?n=9418" width="83px" height="83px" />

To this - notice src value has changed 对此 - 请注意src值已更改

<img src="/IMG_3556E__sq2.jpg?width=83&amp;height=83" id="/IMG_3556E__sq2.jpg?n=9418" width="83px" height="83px" />

This is my code so far. 到目前为止这是我的代码。 All I need is help within the if statement to say if the img tag contains a width or a height, append those to the src attribute. 我需要的只是if语句中的帮助来说明img标记是否包含宽度或高度,将它们附加到src属性。

ContentManager contentManager = new ContentManager();
ContentData Content = contentManager.GetItem(id);

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(Content.Html);

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//img/@src"))
{
    if (//img has a width or height, it means image has been resized) {
        //append that nodes src within the content.html with the ?width=x&height=x
    }
}

Try this: 尝试这个:

static void Main(string[] args)
{
    var htmlDoc = new HtmlDocument();
    htmlDoc.Load(@"E:\Libs\HtmlAgilityPack.1.4.0\htmldoc.html");

    foreach(HtmlNode node in htmlDoc.DocumentNode
                                   .SelectNodes("//img[@src and (@width or @height)]"))
    {
        var src = node.Attributes["src"].Value.Split('?');

        var width = node.Attributes["width"].Value.Replace("px", "");

        var height = node.Attributes["height"].Value.Replace("px", "");

        node.SetAttributeValue("src",
                                src[0] +
                                string.Format("?width={0}&height{1}", width, height));
    }
}

If you use an XPath that selects only nodes with src and width or height, you can omit the if: 如果使用仅选择具有src和宽度或高度的节点的XPath,则可以省略if:

foreach (HtmlNode node in doc.DocumentNode
    .SelectNodes("//img[@src and (@width or @height)]"))
{
    node.SetAttributeValue("src",  ...);
}

but be careful: SelectNodes returns null, if nothing matches - as far as I remember HtmlAgilityPack. 但要小心:SelectNodes返回null,如果没有匹配 - 据我记得HtmlAgilityPack。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM