7

I am trying to create a xml-structre from a name/value-pairs. This works with a xmlwriter. Now I would like to transform this xml.

I think that the best way is to use the xmlwriter as source for the xmlreader to to the transform. But I don't know how to set the xmlwriter as source for the xmlreader.

How can I do this?

user1099480
  • 187
  • 1
  • 4
  • 11

1 Answers1

5

You could use a MemoryStream for example.

MemoryStream stream = new MemoryStream();
using (XmlWriter writer = XmlWriter.Create(stream))
{
    // Do some stuff with writer
}

stream.Seek(0, SeekOrigin.Begin); // Reset stream position to read from the beginning.

using (XmlReader reader = XmlReader.Create(stream))
{
    // Do some stuff with reader
}
stop-cran
  • 4,229
  • 2
  • 30
  • 47
Martin Tausch
  • 714
  • 5
  • 20
  • Thanks a lot. Now I can compile it. But when I use stream.ToString() the result is empty. How can I read the String? (sorry newbie) – user1099480 Oct 10 '13 at 08:42
  • 1
    You can use the reader instead. Everything you write into the stream using the writer, you can read inside the reader with its ReadInnerXML-Method (returns a string). – Martin Tausch Oct 10 '13 at 09:13
  • 1
    Two corrections - either call `writer.Flush()` or dispose `writer` before reading from `stream`. Also call `stream.Seek(0, SeekOrigin.Begin)` before reading to reset the stream position to the beginning. – stop-cran Oct 12 '16 at 07:23