Mar 31, 2015

How to convert an XML file into Byte array in c#?

public static byte[] XMLtoByte(string xmlFileName)
{
   FileStream fs = File.OpenRead(xmlFileName);
   byte[] bytes = ReadWholeArray(fs);
   fs.Close();
   return bytes;
}

public static byte[] ReadWholeArray(Stream stream)
{
   byte[] data = new byte[stream.Length];
   int offset = 0;
   int remaining = data.Length;
   while (remaining > 0)
   {
      int read = stream.Read(data, offset, remaining);
      if (read <= 0)
         throw new EndOfStreamException(String.Format("End of stream reached with {0} bytes left to read", remaining));
      remaining -= read;
      offset += read;
   }
   return data;

}

No comments: