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

如何使用c#将html标记替换为另一个字符串?,c#,asp.net,asp.net-mvc-4,asp.net-mvc-5,html-agility-pack,C#,Asp.net,Asp.net Mvc 4,Asp.net Mvc 5,Html Agility Pack,我有一个c#代码,它将读取html文件并以字符串/文本的形式返回其内容 我需要做的一件事是解析html字符串,查找所有标记,获取“src”属性中的值,然后用src标记中找到的文件内容替换整个标记 我试图使用HtmlAgilityPack来解析html代码 我唯一不能做的事情是如何用另一个字符串替换标记,最后将没有标记的新字符串返回给用户 这就是我所做的 protected string ParseContent(string content) { if (cont

我有一个c#代码,它将读取html文件并以字符串/文本的形式返回其内容

我需要做的一件事是解析html字符串,查找所有
标记,获取“src”属性中的值,然后用
src
标记中找到的文件内容替换整个
标记

我试图使用
HtmlAgilityPack
来解析html代码

我唯一不能做的事情是如何用另一个字符串替换
标记,最后将没有
标记的新字符串返回给用户

这就是我所做的

    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;
    }
受保护的字符串内容(字符串内容)
{
如果(内容!=null)
{
//创建一个新的文档解析器对象
HtmlAgilityPack.HtmlDocument document=新的HtmlAgilityPack.HtmlDocument();
//加载内容
document.LoadHtml(内容);
//获取所有嵌入标记
IEnumerable EmbeddeNodes=document.DocumentNode.substands(“嵌入”);
//确保内容至少包含一个标记
if(embeddeNodes.Count()>0)
{
//输出外部链接的href
foreach(嵌入节点中的HtmlNode嵌入节点)
{
//确保有来源
if(embedNode.Attributes.Contains(“src”))
{
//如果文件以“.html”结尾
if(embedNode.Attributes[“src”].Value.EndsWith(“.html”))
{
var newContent=GetContent(embedNode.Attributes[“src”].Value);
//在这里,我需要能够用newContent替换entireembedNode
}
}
}
}
返回内容;
}
返回null;
}
受保护的字符串GetContent(字符串路径)
{
if(System.IO.File.Exists(path))
{
//文件已存在,请读取其内容
返回System.IO.File.ReadAllText(路径);
}
返回null;
}

如何用字符串替换
标记?

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

var newContent=GetContent(embedNode.Attributes[“src”].Value);
var ParentNodeT=embedNode.ParentNode;
var newNodeTtext=“”+newContent+”

”; var newNodeT=HtmlNode.CreateNode(newNodeStr); parentNode.ReplaceChild(newNode,embeddeNode);
我认为您可以尝试获取当前节点的父节点,即
,然后替换父节点的子节点,即

var newContent=GetContent(embedNode.Attributes[“src”].Value);
var ParentNodeT=embedNode.ParentNode;
var newNodeTtext=“”+newContent+”

”; var newNodeT=HtmlNode.CreateNode(newNodeStr); parentNode.ReplaceChild(newNode,embeddeNode);
我想出来了。 感谢@COlD告诉我,他建议我将枚举转换为列表

这就是我所做的

    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;
    }
受保护的字符串内容(字符串内容)
{
如果(内容!=null)
{
//创建一个新的文档解析器对象
HtmlAgilityPack.HtmlDocument document=新的HtmlAgilityPack.HtmlDocument();
//加载内容
document.LoadHtml(内容);
//获取所有嵌入标记
List embeddenodes=document.DocumentNode.substands(“嵌入”).ToList();
//确保内容至少包含一个标记
if(embeddeNodes.Count()>0)
{
//输出外部链接的href
foreach(嵌入节点中的HtmlNode嵌入节点)
{
//确保有来源
if(embedNode.Attributes.Contains(“src”))
{
if(embedNode.Attributes[“src”].Value.EndsWith(“.html”))
{
//此时,我们知道嵌入标记的源已经设置好,它是一个html文件
//获取完整路径
字符串embedPath=customBase+embedNode.Attributes[“src”].Value;
//得到
字符串newContent=GetContent(嵌入路径);
if(newContent!=null)
{
//创建占位符div节点
HtmlNode newNode=document.CreateElement(“div”);
//此时,我们知道该文件存在,请加载其内容
newNode.InnerHtml=HtmlDocument.HtmlEncode(newContent);
//在这里,我需要能够用newContent替换entireembedNode
document.DocumentNode.InsertAfter(newNode,embeddenode);
//转换代码后将其删除
embeddenode.Remove();
}
}
}
}
return document.DocumentNode.OuterHtml;
}
返回内容;
}
返回null;
}
我想出来了。 感谢@COlD告诉我,他建议我将枚举转换为列表

这就是我所做的

    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;
    }
受保护的字符串内容(字符串内容)
{
如果(内容!=null)
{
//创建一个新的文档解析器对象
HtmlAgilityPack.HtmlDocument document=新的HtmlAgilityPack.HtmlDocument();
//加载内容
document.LoadHtml(内容);
//获取所有嵌入标记
锂