Q:

The best way to validate XML in a unit test?

I have a class with a ToString method that produces XML. I want to unit test it to ensure it is producing valid xml. I have a DTD to validate the XML against. 
Should I include the DTD as a string within the unit test to avoid a dependency on it, or is there a smarter way to do this?

A:

If your program validates the XML against the DTD during normal execution, then you should just get the DTD from wherever your program will get it.
If not and the DTD is extremely short (only a few lines), then storing it as a string in your code is probably okay.
Otherwise, I'd put it in an external file and have your unit test read it from that file.

A:

I've used XmlUnit in the past and found it to be useful.
It can be used to validate XML against a schema or compare your XML against a string. It is clever enough to understand XML's parsing rules.
For example it knows that "<e1/>" is equivalent to "<e1></e1>" and can be configured to ignore or include whitespace.

