views:

472

answers:

3

I am using the (.NET 3.5 SP1) System.Xml.Linq namespace to populate an html template document with div tags of data (and then save it to disk). Sometimes the div tags are empty and this seems to be a problem when it comes to HTML. According to my research, the DIV tag is not self-closing. Therefore, under Firefox at least, a <div /> is considered an opening div tag without a matching closing tag.

So, when I create new div elements by declaring:

XElement divTag = new XElement("div");

How can I force the generated XML to be <div></div> instead of <div /> ?

+1  A: 

Hi

I don't know the answer to your question using LINQ. But there is a project called HTML Agility Pack on codeplex that allows you to create and manipulate HTML documents much similar to the way we can manipulate XML document using System.Xml namespace classes.

O. Askari
+3  A: 

I'm not sure why you'd end up with an empty DIV (seems a bit pointless!) But:

divTag.SetValue(string.Empty);

Should do it.

Steven Robbins
Yah, I it's kinda pointless. I was just being lazy and didn't want to write a bunch of if-checks to determine if I should write those tags or not to the underlying document.
Matthew Ruston
+2  A: 

With XElement divTag = new XElement("div", String.Empty); you get the explicit closing tag

Daniele Armanasco