Search code examples
c#encodingxmlreaderxmlwriter

XmlWriter.WriteNode(XmlReader) writes invalid XML using memory streams


I have a need to normalize XML streams to UTF-16. I use the following method:

All streams passed are byte streams: MemoryStream or FileStream. My problem is when I pass in a filestream containing the following (correctly encoded) XML as jobTicket:

   <?xml version="1.0" encoding="utf-8"?>
   <workflow>
      <file>
        <request name="create-temp-file" tag="очень">
        </request>
        <request name="create-temp-folder" tag="非常に">
        </request>
      </file>
   </workflow>

ticketStreamU16 contains an XML declaration, complete with UTF-8 encoding declaration as UTF-16. This is not well formed XML.

    public void EncodeJobTicket(Stream jobTicket, Stream ticketStreamU16)
    {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.Unicode;

            if (jobTicket.CanSeek)
            {
                jobTicket.Position = 0;
            }

            using (XmlReader xmlRdr = XmlReader.Create(jobTicket))
            using (XmlWriter xmlWtr = XmlWriter.Create(ticketStreamU16, settings))
            {
                xmlWtr.WriteNode(xmlRdr, false);
            }
    }

What am I missing? Shouldn't xmlWtr write the correct xml declartion? Do I have to look for a declaration and replace it?


Solution

  • You should remove the xml declaration from the read stream, because it copy it without change. Xml writer will write correct one for you:

    using (XmlWriter xmlWtr = XmlWriter.Create(ticketStreamU16, settings))
    {
        xmlRdr.MoveToContent();
        xmlWtr.WriteNode(xmlRdr, false);
    }