How to get numbers from HTML in C# by using ID

I want to get some part of an HTML string by using the ID from the Chrome dev tool the f12. I'm using this code to get the HTML downloaded and now I'm pretty much stuck.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Windows;
namespace Matematik_fesser_hack_legit
{
    class Program
    {
        static void Main(string[] args)
        {

            WebClient webCL = new WebClient();
            string html = webCL.DownloadString("https://www.matematikfessor.dk/homework/view/5758111");
            Console.ReadKey();
        }
    }
}

For example I want to get

<pre>
    <span class="mrow" id="MathJax-Span-3">
        <span class="mn" id="MathJax-Span-4" style="font-family: MathJax_Main;">1000</span>
        <span class="mo" id="MathJax-Span-5" style="font-family: MathJax_Main; padding-left: 0.261em; padding-right: 0.261em;">ยท</span>
        <span class="mn" id="MathJax-Span-6" style="font-family: MathJax_Main;">6,2143</span>
        <span class="mo" id="MathJax-Span-7" style="font-family: MathJax_Main; padding-left: 0.347em; padding-right: 0.347em;">=</span>
    </span>
</pre>

which id on the site = MathJas-Span-3 although just the numbers like 6,2143

1 answer

  • answered 2018-01-11 23:16 James

    Load your html into an XmlDocument. Select the node and value.

    void Main()
    {
        string htmlPageContent = "<pre><span class=\"mrow\" id=\"MathJax-Span-3\"><span class=\"mn\" id=\"MathJax-Span-6\" style=\"font-family: MathJax_Main;\">6,2143</span></span></pre>";
        XmlDocument xdoc = new XmlDocument();
        xdoc.LoadXml(htmlPageContent);
    
        var xmlNode= xdoc.SelectSingleNode("//span[@id='MathJax-Span-6']");
        var value = xmlNode.InnerText;
        Console.Write(value);
    }
    

    outputs 6,2143