After spending a very long day with the
System.Xml.Linq namespaces I now understand why the Expando object and JSON were invented!
What I wanted to achieve was quite simple – I thought: read an existing XLIFF file from top to bottom looking for any
trans-units, process those in some way, write the modified versions back into the XLIFF and leave everything else as it was.
The plan: use an
XmlTextWriter in unison. This approach would utilise streaming and thus deal with large files.
It turns out that if you have multiple namespaces in your files this gets messy quickly, particularly if you construct modified portions of the XML as isolated fragments and then try to integrate them again. I was ending up with all kinds of locally declared namespaces though at the end of the day’s work I figured this could have been because I was looking at the non-reintegrated fragments. Perhaps if I’d been more patient and waited until I had a final integrated document they would have all been resolved correctly. Also, the methods on
XmlTextWriter means you end up with very verbose code.
My final implementation used
XmlTextReader for reading the existing file and then
XDocument and Linq to build and write out the modified version. Linq to XML is just so elegant.
Trying to build simple namespace declarations and enforcement of prefix usage in the main file seemed overly difficult with both
Sytsem.Xml and Linq.