我无法使用 C# 敏捷包解析此 html 代码

I can't parse this html code using C# agility pack

我已经尝试解析这段代码很长时间了:

<html>
<body class="detailpage">
    <div id="innerLayout">
        <section id="body-container">
            <div class="wrapper">
                <div class="content" id="offer_active">
                    <div class="clr offerbody">
                        <div class="offercontent fleft rel ">
                            <div class="offercontentinner">
                                <script>
                                    texto = {"name":"John"};
                                </script>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        
    </div>
</body>
</html>

我比较喜欢用AgilityPack,结果想得到"name" : "John",但是一直没有成功

这是我的尝试:

string stringThatKeepsYourHtml = @"<!DOCTYPE html> <head> <title>Title</title> </head> <body> <div id=""myId"" class=""myClass""> <div class=""myClass"">hello</div> </div> </body> </html>"; 
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(stringThatKeepsYourHtml); 
string whatUrLookingFor = doc.DocumentNode.
    SelectNodes("//div").
    First().
    SelectNodes("//div").
    First().
    InnerText; 
Console.WriteLine(whatUrLookingFor); 
Console.ReadKey(true);

我怎样才能让它工作?

不确定解析它的问题是什么。这工作正常:


        var html = @"
<html>
<body class=""detailpage"">
    <div id=""innerLayout"">
        <section id=""body-container"">
            <div class=""wrapper"">
                <div class=""content"" id=""offer_active"">
                    <div class=""clr offerbody"">
                        <div class=""offercontent fleft rel "">
                            <div class=""offercontentinner"">
                                <script>
                                    texto = {""name"":""John""};
                                </script>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        
    </div>
</body>
</html>";

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);

        string scr = htmlDoc.DocumentNode.SelectSingleNode("//script").InnerText;
        
        Console.WriteLine(scr);

scr 包含完整的脚本 texto = {"name":"John"} - 您可以删除 texto = 然后 json 解析其余部分,或者只取 { 之间的所有内容和 } 使用一些子字符串,例如:

var openBra = scr.IndexOf('{');
var closeBra = scr.LastIndexOf('}');
var between = scr[openBra+1..closeBra]; //c# version 8 ranges feature, use Substring if you're on c# <8

我不太清楚你想用它做什么

https://dotnetfiddle.net/Uinjl6