New User? Register here - Existing Users: Username: Password: [Advanced Login]

 

 

Current Poll

What language for system administration do you use ?







( 731 votes ~ 3 comments )

 

Weblog entry #1 for diolt

Combine nodes with Html Agility Pack
Posted by diolt on Tue 3 Jan 2012 at 15:18
Tags: none.

Here's a sample MVC Controller action: public ActionResult Index() { return View(); }

We'll start by making the following changes... I want to turn it into this, instead:

Here's a sample MVC Controller action:

        public ActionResult Index()
    {
        return View();
    }

We'll start by making the following changes... I ended up writing a brute-force loop that iterates nodes looking for consecutive ones, but this seems ugly to me: HtmlDocument doc = new HtmlDocument(); doc.Load(file); var nodes = doc.DocumentNode.ChildNodes; string contents = string.Empty; foreach (HtmlNode node in nodes) { if (node.Name == "code") { contents += node.InnerText + Environment.NewLine; if (node.NextSibling.Name != "code" && !(node.NextSibling.Name == "#text" && node.NextSibling.NextSibling.Name == "code") ) { node.Name = "pre"; node.Attributes.RemoveAll(); node.SetAttributeValue("class", "brush: csharp"); node.InnerHtml = contents; contents = string.Empty; } } } nodes = doc.DocumentNode.SelectNodes(@"//code"); foreach (var node in nodes) { node.Remove(); } Normally I'd remove the nodes in the first loop, but that doesn't work during iteration since you can't change the collection as you iterate over it. Better ideas?

http://www.diolt.com

 

 

 

Flattr