如何使用c#将html标记替换为另一个字符串?

时间:2016-02-18 20:35:59

标签: c# asp.net asp.net-mvc-4 asp.net-mvc-5 html-agility-pack

我有一个c#代码,它将读取一个html文件并将其作为字符串/文本返回。

我需要做的一件事是解析html字符串,查找所有<embed>标记,获取“src”属性中的值,然后将整个<embed>标记替换为在src标记中找到的文件。

我正在尝试使用HtmlAgilityPack来解析html代码。

我唯一无法做的是如何用另一个字符串替换<embed>标记,最后将没有<embed>标记的新字符串返回给用户。

这就是我所做的

    protected string ParseContent(string content)
    {
        if (content != null)
        {
            //Create a new document parser object
            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();

            //load the content
            document.LoadHtml(content);

            //Get all embed tags
            IEnumerable<HtmlNode> embedNodes = document.DocumentNode.Descendants("embed");

            //Make sure the content contains at least one <embed> tag
            if (embedNodes.Count() > 0)
            {
                // Outputs the href for external links
                foreach (HtmlNode embedNode in embedNodes)
                {
                    //Mak sure there is a source
                    if (embedNode.Attributes.Contains("src"))
                    {
                        //If the file ends with ".html"
                        if (embedNode.Attributes["src"].Value.EndsWith(".html"))
                        {
                            var newContent = GetContent(embedNode.Attributes["src"].Value);

                            //Here I need to be able to replace the entireembedNode with the newContent
                        }

                    }
                }
            }

            return content;
        }

        return null;
    }

    protected string GetContent(string path)
    {

        if (System.IO.File.Exists(path))
        {
            //The file exists, read its content
            return System.IO.File.ReadAllText(path);
        }

        return null;
    }

如何用字符串替换<embed>标记?

2 个答案:

答案 0 :(得分:1)

我认为您可以尝试获取当前节点的父节点<embed>,然后替换父<embed>的子节点

var newContent = GetContent(embedNode.Attributes["src"].Value);
var ParentNodeT =embedNode.ParentNode;
var newNodeTtext = "<p>"+newContent+"</p>";
var newNodeT = HtmlNode.CreateNode(newNodeStr);
ParentNodeT.ReplaceChild(newNodeT, embedNode);

答案 1 :(得分:1)

我明白了。 感谢@COlD TOLD,他建议我将enumerable转换为list

这就是我所做的。

    protected string ParseContent(string content)
    {
        if (content != null)
        {
            //Create a new document parser object
            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();

            //load the content
            document.LoadHtml(content);

            //Get all embed tags
            List<HtmlNode> embedNodes = document.DocumentNode.Descendants("embed").ToList();

            //Make sure the content contains at least one <embed> tag
            if (embedNodes.Count() > 0)
            {
                // Outputs the href for external links
                foreach (HtmlNode embedNode in embedNodes)
                {
                    //Mak sure there is a source
                    if (embedNode.Attributes.Contains("src"))
                    {

                        if (embedNode.Attributes["src"].Value.EndsWith(".html"))
                        {
                            //At this point we know that the source of the embed tag is set and it is an html file


                            //Get the full path
                            string embedPath = customBase + embedNode.Attributes["src"].Value;

                            //Get the 
                            string newContent = GetContent(embedPath);

                            if (newContent != null)
                            {
                                //Create place holder div node
                                HtmlNode newNode = document.CreateElement("div");

                                //At this point we know the file exists, load it's content
                                newNode.InnerHtml = HtmlDocument.HtmlEncode(newContent);

                                //Here I need to be able to replace the entireembedNode with the newContent
                                document.DocumentNode.InsertAfter(newNode, embedNode);

                                //Remove the code after converting it
                                embedNode.Remove();
                            }
                        }

                    }
                }

                return document.DocumentNode.OuterHtml;
            }

            return content;
        }

        return null;
    }