How to read the value of an html block in C #?

1

I need to pass a block of html that has a label with a path of an image (this block is saved in the database), the path of the image I need to put in a function of c #.

I already get the html as a string but I can not extract the value of the element

//Esta es la conexion aqui si muestra el bloque html que viene de la base de datos
cone.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cone;
        cmd.CommandText = "SELECT encabezado from dbo.cuentas where codigo =  '" + codigoCuentas + "'";

        reader = cmd.ExecuteReader();
        //reader.Read();

        //cmd.CommandType = CommandType.Text;



        if (reader.Read())
        {
            //Console.WriteLine(String.Format("{0}", reader["nombre"]));
            encabezado = reader["encabezado"].ToString();
            prueba.Text = nombre;
            cone.Close();
        }

Block html showing as string

<div style="text-align: center;" align="center"><br></div><div style="text-align: center;"
align="center"><img src="/pcaAdmin/Uploads/PCA 3 peq.png"><br></div><hr>

What I need is to put the route of the img src in this code

iTextSharp.text.Image logoTims = iTextSharp.text.Image.GetInstance("Aqui va la ruta");
    
asked by Arturo 25.01.2018 в 16:29
source

2 answers

1

You should try something like this ...

Since your string is almost "static", so it has the same form, and the div only has one image (that we hope!), something like that should give you back what you're looking for:

s = s.Substring(s.IndexOf("<img src=") + 10);
s = s.Substring(0, s.IndexOf('"'));

where s is the string that your DIV originally had. You may have to adjust some of the indices (I did it without trying it), but usually it will return the string you are looking for.

    
answered by 25.01.2018 / 16:58
source
1

If the HTML block is always going to have the same structure, you can use regular expressions (Regexp) to get the value you need. It is a complex issue but very useful for cases in which the structure always has the same shape.

In a regexp you can compare any text against a pattern and see if it fits. If you fit, you can capture groups of results (those indicated in parentheses). There are many examples on MSDN and other sites .

A real example of an app:

        // en la variable content tenemos la página en HTML. El formato es:
        // <tr class="par"><td><a href="/events/sensibles/2014/03/11-1845-28L.S201403.html" target="centro">2014/03/11 15:45:29</a></td><td>53 km al NO de San Pedro de Atacama</td><td>4.0 Ml</td></tr>

        const string pattern =
            "<tr class=\"[^>]*\"><td><a href=\"([^\"]*)\" [^>]*>([^<]*)</a></td><td>([^<]*)</td><td>([^<]*)</td><td>([^<]*)</td><td>([^<]*)</td><td>([^<]*)</td><td>([^<]*)</td><td>([^<]*)</td></tr>";
        var reg = new Regex(pattern);

        Match m = reg.Match(content);

        while (m.Success == true)
        {
            var t = new Temblor(id, rkey);
            DateTime tmp;  
            // capturar fecha y hora del temblor
            if (DateTime.TryParse(m.Groups[3].Value, new CultureInfo("es-CL"),
                    DateTimeStyles.AssumeUniversal,
                    out tmp) == true)
                    t.Time = tmp;
            list.Add(t);
            m = m.NextMatch();
        }
    
answered by 25.01.2018 в 17:08