I’m trying to use System.Xml.Linq to create XHTML documents. Thus, the vast majority of the nodes in my trees ought to use this namespace:
http://www.w3.org/1999/xhtml
I can create XElement nodes scoped to this namespace easily enough, using an XNamespace, like this:
XNamespace xhtml = 'http://www.w3.org/1999/xhtml'; // ... new XElement(xhtml + 'html', // ...
However, I don’t want to have to make an XNamespace available throughout all the code that creates HTML nodes, and have to prefix every single XElement (and XAttribute) name I create accordingly.
The XML text format itself takes this requirement into account, and permits setting a default namespace in an ancestor which is inherited by descendants, using the reserved xmlns attribute. I’d like to do something similar using System.Xml.Linq.
Is this possible?
I’ve decided to use a static class called
XHtml, that looks like this:This seems to be the cleanest way of doing things, particularly as I can then add in convenience routines, such as the
XHtml.Amethod (not all of my class is shown here).