XmlReader / Writer are sequential access threads. You will need to read on one end, process the stream as you want, and write it on the other end. The advantage is that you do not need to read all this in memory and create the DOM, which you will get with any XmlDocument-based approach.
This method should start working:
private static void PostProcess(Stream inStream, Stream outStream) { var settings = new XmlWriterSettings() { Indent = true, IndentChars = " " }; using (var reader = XmlReader.Create(inStream)) using (var writer = XmlWriter.Create(outStream, settings)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: writer.WriteStartElement(reader.Prefix, reader.Name, reader.NamespaceURI); writer.WriteAttributes(reader, true);
It's not as clean as getting your own XmlWriter, but I find it a lot easier.
[EDIT]
An example of how you could open two streams at once might be something like this:
using (FileStream readStream = new FileStream(@"c:\myFile.xml", FileMode.OpenOrCreate, FileAccess.Read, FileShare.Write)) { using (FileStream writeStream = new FileStream(@"c:\myFile.xml", FileMode.OpenOrCreate, FileAccess.Write)) { PostProcess(readStream, writeStream); } }
Alex j
source share