Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找具有特定HTML类名的图像_C#_Asp.net_Regex - Fatal编程技术网

C# 查找具有特定HTML类名的图像

C# 查找具有特定HTML类名的图像,c#,asp.net,regex,C#,Asp.net,Regex,我有一些包含特定HTML图像标记的标记,这些标记具有类的特征。我需要的是找到所有这些图像,在图像周围添加一个锚定标记,将锚定的href属性设置为imagessrc值(图像路径),最后用一个新值替换imagessrc值(我调用一个将返回此值的方法) 这里有一些文本。还有一些不应该修改的文本和另一个图像 应该成为 <p>Some text here <a href="/my/path/image.png"><img src="/new/path/from/method

我有一些包含特定HTML图像标记的标记,这些标记具有类的特征。我需要的是找到所有这些图像,在图像周围添加一个锚定标记,将锚定的href属性设置为imagessrc值(图像路径),最后用一个新值替换imagessrc值(我调用一个将返回此值的方法)

这里有一些文本。还有一些不应该修改的文本和另一个图像

应该成为

<p>Some text here <a href="/my/path/image.png"><img src="/new/path/from/method.png" alt="image description" class="featured" /></a>. Some more text and another image that should not be modified <img src="/my/path/image2.png" alt="image description" /></p>
这里有一些文本。还有一些不应该修改的文本和另一个图像


不要使用正则表达式解析HTML。请参阅经典,然后回答原因


改为使用XPath-您可以使用XPath查询HTML。

最后是这段代码

using System;
运用系统反思; 使用HtmlAgilityPack; 使用log4net

命名空间Company.Web.Util { 公共静态类HTMLPasser { 私有静态只读ILog _log=LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 私有静态HtmlDocument _HtmlDocument

    public static string Parse(string input)
    {
        _htmlDocument = new HtmlDocument();

        _htmlDocument.LoadHtml(input);
        ParseNode(_htmlDocument.DocumentNode);

        return _htmlDocument.DocumentNode.WriteTo().Trim();
    }

    private static void ParseChildren(HtmlNode parentNode)
    {
        for (int i = parentNode.ChildNodes.Count - 1; i >= 0; i--)
        {
            ParseNode(parentNode.ChildNodes[i]);
        }
    }

    private static void ParseNode(HtmlNode node)
    {
        if (node.NodeType == HtmlNodeType.Element)
        {
            if (node.Name == "img" && node.HasAttributes)
            {
                for (int i = node.Attributes.Count - 1; i >= 0; i--)
                {
                    HtmlAttribute currentAttribute = node.Attributes[i];
                    if ("class" == currentAttribute.Name && currentAttribute.Value.ToLower().Contains("featured"))
                    {
                        try
                        {
                            string originaleImagePath = node.Attributes["src"].Value;

                            string imageThumbnailPath = GetImageThumbnail(originaleImagePath);

                            var anchorNode = HtmlNode.CreateNode("<a>");
                            var imageNode = HtmlNode.CreateNode("<img>");

                            imageNode.SetAttributeValue("alt", node.Attributes["alt"].Value);
                            imageNode.SetAttributeValue("src", imageThumbnailPath);

                            anchorNode.SetAttributeValue("href", originaleImagePath);

                            anchorNode.AppendChild(imageNode);
                            node.ParentNode.InsertBefore(anchorNode, node);

                            node.ParentNode.RemoveChild(node);
                        }
                        catch (Exception exception)
                        {
                            if (_log.IsDebugEnabled)
                            {
                                _log.WarnFormat("Some message: {0}", exception);
                            }
                        }
                    }
                }
            }
        }

        if (node.HasChildNodes)
        {
            ParseChildren(node);
        }
    }
}
公共静态字符串解析(字符串输入)
{
_htmlDocument=新的htmlDocument();
_htmlDocument.LoadHtml(输入);
ParseNode(_htmlDocument.DocumentNode);
返回_htmlDocument.DocumentNode.WriteTo().Trim();
}
私有静态子节点(HtmlNode parentNode)
{
对于(int i=parentNode.ChildNodes.Count-1;i>=0;i--)
{
ParseNode(parentNode.ChildNodes[i]);
}
}
私有静态void ParseNode(HtmlNode节点)
{
if(node.NodeType==HtmlNodeType.Element)
{
if(node.Name==“img”&&node.HasAttributes)
{
对于(int i=node.Attributes.Count-1;i>=0;i--)
{
HtmlAttribute currentAttribute=node.Attributes[i];
如果(“类”==currentAttribute.Name&¤tAttribute.Value.ToLower()包含(“特征”))
{
尝试
{
string originaleImagePath=node.Attributes[“src”].Value;
字符串imageThumbnailPath=GetImageThumbnailPath(originaleImagePath);
var anchorNode=HtmlNode.CreateNode(“”);
var imageNode=HtmlNode.CreateNode(“

}

这可以通过客户端上的jQuery来完成。您是否只需要一个一次性的服务器端查找和替换样式脚本,或者您是否能够通过jQuery在客户端上实现此功能?需要在服务器上执行此操作,以便它适用于未启用JavaScript或未启用JavaScript的用户。谢谢!将研究HTML Agility Pack
    public static string Parse(string input)
    {
        _htmlDocument = new HtmlDocument();

        _htmlDocument.LoadHtml(input);
        ParseNode(_htmlDocument.DocumentNode);

        return _htmlDocument.DocumentNode.WriteTo().Trim();
    }

    private static void ParseChildren(HtmlNode parentNode)
    {
        for (int i = parentNode.ChildNodes.Count - 1; i >= 0; i--)
        {
            ParseNode(parentNode.ChildNodes[i]);
        }
    }

    private static void ParseNode(HtmlNode node)
    {
        if (node.NodeType == HtmlNodeType.Element)
        {
            if (node.Name == "img" && node.HasAttributes)
            {
                for (int i = node.Attributes.Count - 1; i >= 0; i--)
                {
                    HtmlAttribute currentAttribute = node.Attributes[i];
                    if ("class" == currentAttribute.Name && currentAttribute.Value.ToLower().Contains("featured"))
                    {
                        try
                        {
                            string originaleImagePath = node.Attributes["src"].Value;

                            string imageThumbnailPath = GetImageThumbnail(originaleImagePath);

                            var anchorNode = HtmlNode.CreateNode("<a>");
                            var imageNode = HtmlNode.CreateNode("<img>");

                            imageNode.SetAttributeValue("alt", node.Attributes["alt"].Value);
                            imageNode.SetAttributeValue("src", imageThumbnailPath);

                            anchorNode.SetAttributeValue("href", originaleImagePath);

                            anchorNode.AppendChild(imageNode);
                            node.ParentNode.InsertBefore(anchorNode, node);

                            node.ParentNode.RemoveChild(node);
                        }
                        catch (Exception exception)
                        {
                            if (_log.IsDebugEnabled)
                            {
                                _log.WarnFormat("Some message: {0}", exception);
                            }
                        }
                    }
                }
            }
        }

        if (node.HasChildNodes)
        {
            ParseChildren(node);
        }
    }
}