A: 

Yes, I'm using the framework's XML-related classes to parse XHTML, and to create XHTML.

ChrisW
A: 

I think the ironic "congrats, you finally got it" is the most appropriate answer here. XHTML is a subset of XML, so any XML tools would be able to do this properly for you, and allow you to focus on the content, not the format of the content.

Basically, there are more XML parsers and document models than you can shake a stick at. Unless you have a specific need for performance and crafting custom XML strings makes more sense to you, using one of these libraries is the best bet.

casperOne
A: 

Building a DOM dynamically server-side and then dumping the resulting HTML (or XHTML) is what most serious server-side technologies do. Perhaps they don't do it with raw XML--perhaps they have their own class structure/API that they use to do this, but that's the basic paradigm they follow.

As for "congrats, you finally got it", I'd say you're on the right path. You didn't mention specific technology, but I'd guess you've been messing around with creating raw HTML using basic PHP or Perl. Congratulations, you've done what the rest of us do, eventually: wonder if there's a better way. I would suggest that you look into some of the serious web application frameworks out there, like ASP.NET MVC, Ruby on Rails, Struts, or Twisted.

Randolpho
A: 

This is exactly what XSLT was invented for: transforming XML documents into XML documents without screwing anything up. Once you understand XSLT (and, regrettably, this takes some doing), the kinds of problems that are vexing you now disappear from your life completely.

Robert Rossney