Search code examples
c#asp.nethtmliishttpmodule

strip out everything out side of <img src=random.jpg> and <p>random text</p> in html


I am trying to strip data out of a web page using a c# http module. I just want raw text and images. How can I strip everything else out?

private static Regex reg = new Regex(@"<img src=\t????????");

public override void Write(byte[] buffer, int offset, int count)
    {
      byte[] data = new byte[count];
      Buffer.BlockCopy(buffer, offset, data, 0, count);
      string html = System.Text.Encoding.Default.GetString(buffer);

      html = reg.Replace(html, string.Empty);


      byte[] outdata = System.Text.Encoding.Default.GetBytes(html);
      _sink.Write(outdata, 0, outdata.GetLength(0));
    }

Solution

  • Use an HTML parser, such as the HtmlAgilityPack.