Home > Ask the Microsoft .Net Development Experts > C# Development Questions & Answers > Enabling the storage of an audio file in a XML file
Ask The Win Development Expert: Questions & Answers
EMAIL THIS

Enabling the storage of an audio file in a XML file

Mark Belles EXPERT RESPONSE FROM: Mark Belles

Pose a Question
Other Win Development Categories
Meet all Win Development Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 11 August 2004
Is there any way to enable the storage of an audio file in a XML file?

>
EXPERT RESPONSE
Yes, you can store binary data in XML documents. Because of the special characters that are needed by XML, it is necessary to encode the binary data using either the Base64 encoding algorithm, or the hexadecimal encoding algorithm. Read more on this from MSDN here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwebsrv/html/infoset_whitepaper.asp

The System.Convert class contains methods for helping you convert between Base64 and non-Base64 data. Refer to the System.Convert.ToBase64String() and System.Convert.FromBase64String() methods for more information in the MSDN Library.

Here is an example method of how you might read the file in and convert it to a Base64 encoded string. Note that this method is just for demo purposes and I would not recommend reading the entire file in at once like this method is going to show. Read it in segments or asynchronously but never all at once.

 using System;
 using System.Diagnostics;
 using System.IO;
 
 namespace Base64Example
 {
  /// <summary>
  /// Summary description for Base64Encoder.
  /// </summary>
  public class Base64
  {
   /// <summary>
   /// Read the file into a base 64 string (Not an efficient means, just used for demonstration purposes!)
   /// </summary>
   /// <param name="path">The name of the file to read</param>
   /// <returns></returns>
   public static string ReadFile(string path)
   {
    string base64String = string.Empty;
    try
    {
     FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
     if (fs != null)
     {
      byte[] bytes = new byte[fs.Length];
      int bytesRead = fs.Read(bytes, 0, (int)fs.Length);
      if (bytesRead > 0)
      {
       base64String = System.Convert.ToBase64String(bytes);
      }
     }
    }
    catch(Exception ex)
    {
     Trace.WriteLine(ex);
    }
    return base64String;
   }
 
  }
 }
 
Reversing the process should be trivial. Simply take the string and use the FromBase64String method to convert it back to it's original byte[]. Once converted, write it back out to your file.

Cheers,
Mark


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
C# programming language
Mono 2.0 moves .NET apps to Linux - includes migration analyzer
LINQ beyond queries: Strong-typed refection
Assembly versioning in the .NET Framework 2.0
Book excerpt: Creating graphical output using the .NET Compact Framework
Visual Studio 2008 Learning Guide: C# 3.0
Simonyi firm to address divide between domain experts and developers
On Extension Methods in C# and .NET Framework 3.5
Generate RSA public and private keys, export to XML
Book excerpt: Upgrading to Visual Studio 2005
Learning .NET: Tips for getting started with .NET development

C# Development
Top C# expert advice
Using LoadRunner for testing in .NET
Take care with InitializeComponent
GroupBox inheritance
Escape from mailto:
Windows Forms: Overriding the OnClosing method
Passing data between forms
Can I run a .NET app without installing .NET runtime?
Opening a link with parameters to a new window
Is it possible to open a child form in modal type?

C#
Top C# expert advice
Escape from mailto:
Can I run a .NET app without installing .NET runtime?
Opening a link with parameters to a new window
Is it possible to open a child form in modal type?
Painting in the non-client area
Global cases to the entire application
Validating the textbox entry for date format
Book on network programming with C#
Calling a secondary UserControl applet

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
C#  (SearchWinDevelopment.com)
GLib  (SearchWinDevelopment.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Windows Development - White Papers, News and Expert Advice
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts