Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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# HtmlAgilityPack如何替换此文本cf.example_C#_Html Agility Pack - Fatal编程技术网

C# HtmlAgilityPack如何替换此文本cf.example

C# HtmlAgilityPack如何替换此文本cf.example,c#,html-agility-pack,C#,Html Agility Pack,我想知道如何在字符串中输入一个值。 例如,我想将HELLO值放入字符串中,但我不知道如何操作..:(有人能帮我吗 <div class="position" > <span class="cvetica" style="font-size: 24px"> <label style="font-size: 30px">11.522</label> HELLO </span> </div> 11.522 你好 您

我想知道如何在字符串中输入一个值。 例如,我想将HELLO值放入字符串中,但我不知道如何操作..:(有人能帮我吗

<div class="position" >
<span class="cvetica" style="font-size: 24px">
<label style="font-size: 30px">11.522</label>
HELLO      
</span>
</div>

11.522
你好

您的特定示例相当简单-最新的HtmlAgilityPack还支持Linq,因此这将起作用:

HtmlDocument doc = new HtmlDocument();
doc.Load("test.html");

var spanNode = doc.DocumentNode.Descendants("span")
                    .Where(x => x.Attributes["class"]!=null && x.Attributes["class"].Value == "cvetica")
                    .FirstOrDefault();

if (spanNode != null)
{
    string text = spanNode.InnerText; 
    string  textHello = spanNode.ChildNodes
                                .Where(x => x.NodeType == HtmlNodeType.Text)
                                .Last().InnerText;
}

请注意,第一个输出可能不是您所期望的,因为字符串“HELLO”没有直接的封闭元素,因此您也将获得标签的HTML-您可以通过检索最后一个文本节点来解决这个问题。

@Jérémiedjil'Amoroso:使用示例更新以直接检索最后一个文本节点