C# 在c中重命名html文件的属性#

C# 在c中重命名html文件的属性#,c#,.net,html-parsing,C#,.net,Html Parsing,我在一个字符串变量中有一个HTML,它包含一些带有data realsrc属性的属性,并将load.gif放在src属性中 例如,如果这是 我想将其更改为: <img src="Hello.jpg" /> 它应该应用于HTML中的所有标记 请注意,我想在服务器端用c#实现 怎么做 最简单的方法是什么?最快的方法是什么?使用 HtmlAgilityPack.HtmlDocument doc=新的HtmlAgilityPack.HtmlDocument(); doc.LoadHt

我在一个字符串变量中有一个
HTML
,它包含一些带有
data realsrc
属性的
属性,并将
load.gif
放在
src
属性中

例如,如果这是

我想将其更改为:

<img src="Hello.jpg" />

它应该应用于
HTML
中的所有
标记

请注意,我想在服务器端用c#实现

怎么做

最简单的方法是什么?最快的方法是什么?

使用

HtmlAgilityPack.HtmlDocument doc=新的HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(@“);
foreach(doc.DocumentNode.substands(“img”)中的var img)
{
var realSource=img.Attributes[“数据realsrc”];
if(realSource!=null)
realSource.Value=img.Attributes[“src”].Value;
其他的
Add(“data realsrc”,img.Attributes[“src”].Value);
img.Attributes[“src”].Value=“load.gif”;
}
使用

HtmlAgilityPack.HtmlDocument doc=新的HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(@“);
foreach(doc.DocumentNode.substands(“img”)中的var img)
{
var realSource=img.Attributes[“数据realsrc”];
if(realSource!=null)
realSource.Value=img.Attributes[“src”].Value;
其他的
Add(“data realsrc”,img.Attributes[“src”].Value);
img.Attributes[“src”].Value=“load.gif”;
}

您是否尝试过字符串.Replace?@AVD页面中应该有许多
src=
s。(示例:
您是否尝试过字符串.Replace?@AVD页面中应该有许多
src=
s。(示例:

<img data-realsrc="Hello.jpg" src="loading.gif" />
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(@"<img src=""Hello.jpg"" />");

foreach (var img in doc.DocumentNode.Descendants("img"))
{
    var realSource = img.Attributes["data-realsrc"];

    if (realSource != null)
        realSource.Value = img.Attributes["src"].Value;
    else
        img.Attributes.Add("data-realsrc", img.Attributes["src"].Value);

    img.Attributes["src"].Value = "loading.gif";
}