Introducing TextReplacer: A New Class for PowerTools for Open XML
Recently I wrote some code that implemented search-and-replacefor Open XML WordprocessingML documents. I wrote that code for an Open XML developer who needed to implement thatfunctionality using XML DOM, although with a different language than C#. Because XML DOM is standardized,translating the code to another language and another implementation of XML DOMis relatively straightforward.
I want to introduce search-and-replace functionality in a CMDLET in PowerTools for Open XML, but I have been moving PowerTools code away from XmlDocument, so I rewrote the search-and-replace code using LINQ to XML, using a functional transform. It was an interesting and fun project. The video below introduces the TextReplacer class, and compares it to the code that I presented that uses XmlDocument. It is an interesting comparison of imperative code (using XmlDocument) and functional code (using LINQ to XML).
It took me about 8 hours to write the search-and-replace code using XmlDocument. It took me about 4 hourz to write the same code using LINQ to XML. However, for what it’s worth, I spent far longer than that on both versions of the code making sure that whatever else happens, the code will not corrupt WordprocessingML documents.
Personally, I find the functional code to be much cleaner and easier to maintain. It was also easier to debug.
If you want to see a detailed description of the algorithm that I used for search-and-replace, see the previous post on search-and-replace that uses XmlDocument. The approach that I took with the LINQ to XML code is identical in nature to the code that uses XmlDocument. This will be apparent in the video.
I am currently in process of doing some relatively major revamps to PowerTools for Open XML, so I haven’t posted this code on CodePlex yet. You can retrieve the code as an attachment to this post.
Download – Example Code