There is a transformation language for exactly this kind of purpose called
XSLT[
^]
All you need to do is create an XML file using XSLT that describes how the elements and attribute should be transformed.
Then use
the XSLT classes[
^] to perform the transformation.