I have code like this. Is there a way to make it easier to write and maintain? Using C# .NET 3.5.
string header(string title)
{
StringWriter s = new Stri
Use an XDocument
to create the DOM, then write it out using an XmlWriter
. This will give you a wonderfully concise and readable notation as well as nicely formatted output.
Take this sample program:
using System.Xml;
using System.Xml.Linq;
class Program {
static void Main() {
var xDocument = new XDocument(
new XDocumentType("html", null, null, null),
new XElement("html",
new XElement("head"),
new XElement("body",
new XElement("p",
"This paragraph contains ", new XElement("b", "bold"), " text."
),
new XElement("p",
"This paragraph has just plain text."
)
)
)
);
var settings = new XmlWriterSettings {
OmitXmlDeclaration = true, Indent = true, IndentChars = "\t"
};
using (var writer = XmlWriter.Create(@"C:\Users\wolf\Desktop\test.html", settings)) {
xDocument.WriteTo(writer);
}
}
}
This generates the following output:
This paragraph contains bold text.
This paragraph has just plain text.