I am creating an HTML document using HTML agility pack. I load a template file then append content to it. All of this works, but when I view the output file it has removed the closing tag from my <br/>
tags to look like this <br>
. What is causing this?
Dim doc As New HtmlDocument()
doc.Load(Server.MapPath("Template.htm"))
Dim title As HtmlNode = doc.DocumentNode.SelectSingleNode("//title")
title.InnerHtml = title.InnerHtml & "CEU Classes"
Dim topContent As HtmlAgilityPack.HtmlNode = doc.GetElementbyId("topContent")
topContent.InnerHtml = html.ToString
doc.OptionWriteEmptyNodes = True
doc.Save(outputFileName, Encoding.UTF8)
More info:
It was removing my closing image tags, after I added doc.OptionWriteEmptyNodes = True
, it quite doing that.
Update
This is my code as it stands now that removes the closing BR tag
Dim html As String = "Words<br/>more words"
Dim doc As New HtmlDocument()
Dim title As HtmlNode
Dim topContent As HtmlNode
HtmlNode.ElementsFlags("br") = HtmlElementFlag.Empty
doc.Load(Server.MapPath("Template.htm"))
Title = doc.DocumentNode.SelectSingleNode("//title")
title.InnerHtml = title.InnerHtml & "CEU Classes"
topContent = doc.GetElementbyId("topContent")
topContent.InnerHtml = html.ToString
doc.OptionWriteEmptyNodes = True
doc.Save(outputFileName, Encoding.UTF8)
Update 2
I ended up just reading in my template file as a standard string then loading the html like this
Dim TemplateHTML As String = File.ReadAllText(Server.MapPath("Template.htm"))
TemplateHTML = TemplateHTML.Insert(TemplateHTML.IndexOf("<div id=""topContent"">") + "<div id=""topContent"">".Length, _
html.ToString)
doc.LoadHtml(TemplateHTML)
tags are intact. The problem is any
tags in the topcontent part don't have the closing tag. How can I fix that? topContent = doc.GetElementbyId("topContent") topContent.InnerHtml = html.ToString – FarFigNewton Apr 19 '11 at 12:50