i want to be able to send sound file in an xml message, is that possible, how?
thanks.
serkan sendur 821 Postaholic Banned
thoughtcoder 167 Junior Poster
base-64 encode it. base-96 (or whatever) encode it. encode it into unicode characters.
serkan sendur 821 Postaholic Banned
serkan sendur 821 Postaholic Banned
i did it :), i really like this post and i dedicate this post to Marty Friedman especially for his song Forbidden City.
i attach the application to this post. you can click encode to encode any document and then click embed to xml button and save the xml file, then click exract from xml button to create your new file from generated xml document. and verify that your file is not corrupted and works properly. this way of embedding files is useful for sending files over xml messages.
Form1.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace encode_decode_Base64
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnEncode_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtInFile.Text))
{
FileStream fs = new FileStream(txtInFile.Text,
FileMode.Open,
FileAccess.Read);
byte[] filebytes = new byte[fs.Length];
fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
string encodedData =
Convert.ToBase64String(filebytes,
Base64FormattingOptions.InsertLineBreaks);
txtEncoded.Text = encodedData;
}
}
private void btnDecode_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
byte[] filebytes = Convert.FromBase64String(txtEncoded.Text);
FileStream fs = new FileStream(saveFileDialog1.FileName,
FileMode.CreateNew,
FileAccess.Write,
FileShare.None);
fs.Write(filebytes, 0, filebytes.Length);
fs.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtInFile.Text = openFileDialog1.FileName;
}
}
private void btnEmbedXml_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
XmlElement elem = doc.CreateElement("content");
elem.InnerText = txtEncoded.Text;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
XmlWriter writer = XmlWriter.Create(saveFileDialog1.FileName);
elem.WriteTo(writer);
writer.Close();
}
}
private void btnExtractXmL_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
byte[] filebytes = null;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
doc.Load(openFileDialog1.FileName);
filebytes = Convert.FromBase64String(doc.GetElementsByTagName("content")[0].InnerText);
}
if (filebytes != null && saveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
fs.Write(filebytes, 0, filebytes.Length);
fs.Close();
}
}
}
}
namespace encode_decode_Base64
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnEncode = new System.Windows.Forms.Button();
this.btnDecode = new System.Windows.Forms.Button();
this.txtEncoded = new System.Windows.Forms.TextBox();
this.txtInFile = new System.Windows.Forms.TextBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.lblBrowse = new System.Windows.Forms.Label();
this.lblEncoded = new System.Windows.Forms.Label();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.btnEmbedXml = new System.Windows.Forms.Button();
this.btnExtractXmL = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnEncode
//
this.btnEncode.Location = new System.Drawing.Point(288, 87);
this.btnEncode.Name = "btnEncode";
this.btnEncode.Size = new System.Drawing.Size(75, 23);
this.btnEncode.TabIndex = 1;
this.btnEncode.Text = "Encode";
this.btnEncode.UseVisualStyleBackColor = true;
this.btnEncode.Click += new System.EventHandler(this.btnEncode_Click);
//
// btnDecode
//
this.btnDecode.Location = new System.Drawing.Point(288, 415);
this.btnDecode.Name = "btnDecode";
this.btnDecode.Size = new System.Drawing.Size(75, 23);
this.btnDecode.TabIndex = 2;
this.btnDecode.Text = "Decode";
this.btnDecode.UseVisualStyleBackColor = true;
this.btnDecode.Click += new System.EventHandler(this.btnDecode_Click);
//
// txtEncoded
//
this.txtEncoded.Location = new System.Drawing.Point(28, 155);
this.txtEncoded.Multiline = true;
this.txtEncoded.Name = "txtEncoded";
this.txtEncoded.Size = new System.Drawing.Size(381, 228);
this.txtEncoded.TabIndex = 3;
//
// txtInFile
//
this.txtInFile.Location = new System.Drawing.Point(28, 87);
this.txtInFile.Name = "txtInFile";
this.txtInFile.Size = new System.Drawing.Size(161, 20);
this.txtInFile.TabIndex = 5;
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(212, 85);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(43, 23);
this.button1.TabIndex = 6;
this.button1.Text = "...";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// lblBrowse
//
this.lblBrowse.AutoSize = true;
this.lblBrowse.Location = new System.Drawing.Point(35, 57);
this.lblBrowse.Name = "lblBrowse";
this.lblBrowse.Size = new System.Drawing.Size(113, 13);
this.lblBrowse.TabIndex = 7;
this.lblBrowse.Text = "Select a file to encode";
//
// lblEncoded
//
this.lblEncoded.AutoSize = true;
this.lblEncoded.Location = new System.Drawing.Point(35, 126);
this.lblEncoded.Name = "lblEncoded";
this.lblEncoded.Size = new System.Drawing.Size(105, 13);
this.lblEncoded.TabIndex = 8;
this.lblEncoded.Text = "Encoded file content";
//
// btnEmbedXml
//
this.btnEmbedXml.Location = new System.Drawing.Point(28, 464);
this.btnEmbedXml.Name = "btnEmbedXml";
this.btnEmbedXml.Size = new System.Drawing.Size(112, 23);
this.btnEmbedXml.TabIndex = 9;
this.btnEmbedXml.Text = "Embed To XML";
this.btnEmbedXml.UseVisualStyleBackColor = true;
this.btnEmbedXml.Click += new System.EventHandler(this.btnEmbedXml_Click);
//
// btnExtractXmL
//
this.btnExtractXmL.Location = new System.Drawing.Point(180, 464);
this.btnExtractXmL.Name = "btnExtractXmL";
this.btnExtractXmL.Size = new System.Drawing.Size(104, 23);
this.btnExtractXmL.TabIndex = 10;
this.btnExtractXmL.Text = "Extract From XML";
this.btnExtractXmL.UseVisualStyleBackColor = true;
this.btnExtractXmL.Click += new System.EventHandler(this.btnExtractXmL_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(517, 684);
this.Controls.Add(this.btnExtractXmL);
this.Controls.Add(this.btnEmbedXml);
this.Controls.Add(this.lblEncoded);
this.Controls.Add(this.lblBrowse);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtInFile);
this.Controls.Add(this.txtEncoded);
this.Controls.Add(this.btnDecode);
this.Controls.Add(this.btnEncode);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnEncode;
private System.Windows.Forms.Button btnDecode;
private System.Windows.Forms.TextBox txtEncoded;
private System.Windows.Forms.TextBox txtInFile;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label lblBrowse;
private System.Windows.Forms.Label lblEncoded;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Button btnEmbedXml;
private System.Windows.Forms.Button btnExtractXmL;
}
}
This attachment is potentially unsafe to open. It may be an executable that is capable of making changes to your file system, or it may require specific software to open. Use caution and only open this attachment if you are comfortable working with zip files.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.