C# 使用HTML AGILITY PACK更改标记InnerText
我使用HTMLAgilityPack,我试图替换一些标签的内部文本C# 使用HTML AGILITY PACK更改标记InnerText,c#,html,html-agility-pack,C#,Html,Html Agility Pack,我使用HTMLAgilityPack,我试图替换一些标签的内部文本 protected void GerarHtml() { List<string> labels = new List<string>(); string patch = @"C:\EmailsMKT\" + Convert.ToString(Session["ssnFileName"]) + ".html"; D
protected void GerarHtml()
{
List<string> labels = new List<string>();
string patch = @"C:\EmailsMKT\" +
Convert.ToString(Session["ssnFileName"]) + ".html";
DocHtml.Load(patch);
//var titulos = DocHtml.DocumentNode.SelectNodes("//*[@class='lblmkt']");
foreach (HtmlNode titulo in
DocHtml.DocumentNode.SelectNodes("//*[@class='lblmkt']"))
{
titulo.InnerText.Replace("test", lbltitulo1.Text);
}
DocHtml.Save(patch);
}
protected void GerarHtml()
{
列表标签=新列表();
字符串补丁=@“C:\EmailsMKT\”+
Convert.ToString(会话[“ssnFileName”])+“.html”;
加载(补丁);
//var titulos=DocHtml.DocumentNode.SelectNodes(“/*[@class='lblmkt']”);
foreach(HtmlNode titulo in
DocHtml.DocumentNode.SelectNodes(“/*[@class='lblmkt']”)
{
titulo.InnerText.Replace(“test”,lbltitulo1.Text);
}
保存(补丁);
}
html:
<.div><.label id="titulo1" class="lblmkt">teste</label.><./Div>
teste
字符串是不可变的(您应该可以找到很多关于此的文档)。String类的方法不会改变实例,而是创建一个新的、修改过的字符串 因此,您呼吁:
titulo.InnerText.Replace("test", lbltitulo1.Text);
不更改InnerText,但返回您希望InnerText为的字符串
此外,InnerText是只读的;您必须使用中所示的文本
请尝试以下行(再次将字符串操作的结果分配给属性):
我能得到这样的结果:
HtmlTextNode Hnode = null;
Hnode = DocHtml.DocumentNode.SelectSingleNode("//label[@id='titulo1']//text()") as HtmlTextNode;
Hnode.Text = lbltitulo1.Text;
我尝试了,但收到了HtmlAgilityPack.InnerText它仅准备就绪:“无法将类型为“HtmlAgilityPack.HtmlNode”的对象强制转换为类型为“HtmlAgilityPack.HtmlTextNode”。”
HtmlTextNode Hnode = null;
Hnode = DocHtml.DocumentNode.SelectSingleNode("//label[@id='titulo1']//text()") as HtmlTextNode;
Hnode.Text = lbltitulo1.Text;