I know this is kind of obsessive, but is there a way to control the order that the TagBuilder
class renders the attributes of an HTML tag when you call ToString()
?
i.e. so that
var tb = new TagBuilder("meta");
tb.Attributes.Add("http-equiv", "Content-Type");
tb.Attributes.Add("content", "text/html; charset=utf-8");
tb.ToString(TagRenderMode.SelfClosing)
will return
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
not
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
Changing the order that you add the attributes doesn't change it, it seems to be rendering in alphabetical order
Try using this class, which inherits the TagBuilder and overrides the ToString method, building a SortedDictionary from the Attributes and using that dictionary to render.