views:

296

answers:

2

well i have the following problem.
the html i have is malformed and i have problems with selecting nodes using html agility pack when this is the case.
the code is below:

string strHtml = @"
<html>
  <div>
    <p><strong>Elem_A</strong>String_A1_2 String_A1_2</p>
    <p><strong>Elem_B</strong>String_B1_2 String_B1_2</p>
  </div>
  <div>
    <p><strong>Elem_A</strong>String_A2_2 <String_A2_2> asdas</p>
    <p><strong>Elem_B</strong>String_B2_2 String_B2_2</p>
  </div>
</html>";
HtmlAgilityPack.HtmlDocument objHtmlDocument = new HtmlAgilityPack.HtmlDocument();
objHtmlDocument.LoadHtml(strHtml);
HtmlAgilityPack.HtmlNodeCollection colnodePs = objHtmlDocument.DocumentNode.SelectNodes("//p");
List<string> lststrText = new List<string>();
foreach (HtmlAgilityPack.HtmlNode nodeP in colnodePs)
{
  lststrText.Add(nodeP.InnerHtml);
}

the problem is that String_A2_2 is enclosed in brackets.
so htmlagility pack returns 5 strings instead of 4 in the lststrText.
so is it possible to let htmlagility pack return element 3 as "<strong>Elem_A</strong>String_A2_2 <String_A2_2> asdas"?
or maybe i can do some preprocessing to close the element?
the current content of lststrText is

lststrText[0] = "<strong>Elem_A</strong>String_A1_2 String_A1_2"  
lststrText[1] = "<strong>Elem_B</strong>String_B1_2 String_B1_2"  
lststrText[2] = ""  
lststrText[3] = ""  
lststrText[4] = "<strong>Elem_B</strong>String_B2_2 String_B2_2"
+2  A: 

You could use TidyNet to do the pre/postprocessing you allude to. Can you edit your answer to explain why that wouldnt be applicable in your case?

Ruben Bartelink
well i didnt try TidyNet bacause i wasnt able to let it work.but i tried SgmlReader http://developer.mindtouch.com/SgmlReader but i didnt work too.
Karim
+1  A: 

Most html parsers try to build a working DOM, meaning dangling tags are not accepted. They will be converted, or closed in some way.

If only selecting the nodes is of importance to you, and speed and huge amounts of data is not an issue, you could grab all your <p> tags with a regular expression instead:

Regex reMatchP = new Regex(@"<(p)>.*?</\1>");
foreach (Match m in reMatchP.Matches(strHtml))
{
   Console.WriteLine(m.Value);
}

This regular expression assumes the <p> tags are well formed and closed.

If you are to run this Regex a lot in your program you should declare it as:

static Regex reMatchP = new Regex(@"<(p)>.*?</\1>", RegexOptions.Compiled);

[Edit: Agility pack change]

If you want to use HtmlAgility pack you can modify the PushNodeEnd function in HtmlDocument.cs:

if (HtmlNode.IsCDataElement(CurrentNodeName()))
{
   _state = ParseState.PcData;
   return true;
}

// new code start
if ( !AllowedTags.Contains(_currentnode.Name) )
{
    close = true;
}
// new code end

where AllowedTags would be a list of all known tags: b, p, br, span, div, etc.

the output is not 100% what you want, but maybe close enough?

<strong>Elem_A</strong>String_A1_2 String_A1_2
<strong>Elem_B</strong>String_B1_2 String_B1_2
<strong>Elem_A</strong>String_A2_2 <ignorestring_a2_2></ignorestring_a2_2> asdas
<strong>Elem_B</strong>String_B2_2 String_B2_2
Mikael Svenson
regular expressions are not an option to parsehtml.
Karim
I added my code change as an example. It might be of help.
Mikael Svenson
thanks,i will take a look and see if that will fix the problem.
Karim
wow it works, how did u do it ? did you try to understand all the code of html agility pack?
Karim
I've worked with html parsing in .Net for a long time, so it wasn't too hard to debug the code to see what was happening. I'm glad the solution works for you even though it adds a closing tag :)
Mikael Svenson
yeah that explains it , because when i tried to debug htmlagility pack i got lost, anyway you showed me that its possible so i think i will try in the future to fix this problem. but i got other tasks to do right now. thanks
Karim