System.Xml.XmlTextWriter.WriteBase64 C# (CSharp) Method

WriteBase64() public method

public WriteBase64 ( byte buffer, int index, int count ) : void
buffer byte
index int
count int
return void
        public override void WriteBase64(byte[] buffer, int index, int count) {
            try {
                if (!this.flush) {
                    AutoComplete(Token.Base64);
                }

                this.flush = true;
                // No need for us to explicitly validate the args. The StreamWriter will do
                // it for us.
                if (null == this.base64Encoder) {
                    this.base64Encoder = new XmlTextWriterBase64Encoder( xmlEncoder );
                }
                // Encode will call WriteRaw to write out the encoded characters
                this.base64Encoder.Encode( buffer, index, count );
            }
            catch {
                currentState = State.Error;
                throw;
            }
        }

Usage Example

        /// <summary>
        /// Only subscribed keys in the same namespace are saved.
        /// </summary>
        /// <param name="dict"></param>
        /// <returns>XML file</returns>
        public static MemoryStream Save(P2PDictionary dict)
        {
            MemoryStream writeStream = new MemoryStream();

            System.Xml.XmlTextWriter writer = new XmlTextWriter(writeStream,  Encoding.UTF8);
            ICollection<string> keys = dict.Keys;

            writer.WriteStartDocument();
            writer.WriteStartElement("p2pdictionary");
            writer.WriteStartElement("namespace");
            writer.WriteAttributeString("name", dict.Namespace);
            writer.WriteAttributeString("description", dict.Description);

            IFormatter formatter = new BinaryFormatter();

            foreach (string k in keys)
            {
                writer.WriteStartElement("entry");
                writer.WriteAttributeString("key", k);

                using (MemoryStream contents = new MemoryStream())
                {
                    formatter.Serialize(contents, dict[k]);
                    writer.WriteBase64(contents.GetBuffer(), 0, (int)contents.Length);
                }

                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();

            return writeStream;
        }
All Usage Examples Of System.Xml.XmlTextWriter::WriteBase64