how to file stream a dynamically generated xml,I dont want to create file in path ,I want it to write in xml doc RRS feed

  • Question

  •  // XmlDocument doc = new XmlDocument();
                    using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create))
                        int data;
                        while ((data = cs.ReadByte()) != -1)
    Friday, December 22, 2017 9:32 AM

All replies

  • Streams are agnostic. If you want to write to memory use a MemoryStream instead. That memory stream can then be converted to a byte array and passed to whatever. If you're working with strings then use StreamWriter instead. It can write to any string.

    If you want to write to an existing XmlDocument then a stream won't work at all. XmlDocument is an in-memory representation of the XML. You have to convert your "writes" to XmlNode objects so they can be added. Alternatively you can skip the XmlDocument and open the XML file directly but as this is XML you still need to ensure the writes occur in the appropriate place in the document otherwise you'll generate invalid XML.

    If you are trying to write binary data into an XML file then note that it has to be base64 encoded otherwise it'll not be valid XML. So for that you should use a memory stream to store the data in memory. Then base64 convert the results into a string that can be inserted into the document using an XmlElement.

    Michael Taylor

    Friday, December 22, 2017 5:07 PM