Error with Insert before of after

Topics: User Forum
May 27, 2010 at 11:07 AM
Hi,

I have the following text and i need to replace the html structure <ul><li>.....</li></ul> by node text.

I have the following code but i have always a error.

Please who can help me?

In advance thanks,

Max.

PS: i try to find more sample to use html agility but i cannot found it. Do know somebody for a great website with samples?

My code is:

var doc = new HtmlDocument();
            var tw = new StringWriter();
            doc.LoadHtml(text);
            if(doc.DocumentNode.SelectNodes("//ul")!= null)
            {
                foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//ul"))
                {
                    var newNodeUl = new HtmlNode(HtmlNodeType.Text, doc, node.LinePosition);
                    newNodeUl.InnerHtml = "\n";
                    node.ParentNode.InsertAfter(newNodeUl, node);
                    
                    if(node.SelectNodes("//li")!= null)
                    {
                        foreach (HtmlNode htmlNode in node.SelectNodes("//li"))
                        {
                            // replace li by -
                            var newNodeLi = new HtmlNode(HtmlNodeType.Text, doc, htmlNode.LinePosition);
                            newNodeLi.InnerHtml = "- "+ htmlNode.InnerHtml;
                            newNodeUl.ChildNodes.Add(newNodeLi);
                            node.RemoveChild(htmlNode);
                        }
                    }
                    node.Remove();
                }
            }
            doc.Save(tw);
            var returnval = tw.ToString();
            tw.Close();
            return returnval;

 

The text who need to parse is the following:

text texttext text text text text text text text
text texttext text text text

<ul>
<li>1ste option</li>
<li>1ste option</li>
<li>1ste option</li>
</ul>

text text text text text text text text text text 
text text text text text text text text text text 
And I need to have the following:
text texttext text text text text text text text
text texttext text text text

\n
- 1ste option
- 1ste option
- 1ste option
\n

text text text text text text text text text text 
text text text text text text text text text text