I would just read in the file, process and return it back.
This can be done in a streaming way with XmlReader - it is more manual than XmlDocument or XDocument, but it does not allow creating DOM memory in memory (XmlDocument / XDocument can be used with the same read / write pattern, but usually requires a complete reconstruction in memory) :
- Open file stream input file (XmlReader)
- Open stream of output files (XmlWriter, to another file)
- Reading from XmlReader and writing to XmlWriter, performing any conversion as necessary.
- Close streams
- Move new file to old file (overwrite, atomic action)
While this can be configured to handle input and output in the same open file with a bunch of really smart work, nothing will be saved, and there will be many cases with an edge, including an increase with decreasing file length. In fact, it might be slower to just try moving the contents of the file back to fill in the blanks or moving the contents of the file forward to create a new room. The file system cache is likely to make any "winnings" minimal / controversial for anything but the most basic length-saving operation. In addition, changing the file in place is not an atomic action and, as a rule, is not restored in case of an error: due to the temporary file, the read / write / move approach is atomic in the final contents of the file.
Or, consider XSLT - it was designed for that; -)
Happy coding.
user166390
source share