Results 1 to 2 of 2

Thread: base64 encoding Problems **SOLVED**

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    Unhappy base64 encoding Problems **SOLVED**

    Hey all, heres a short description of what I do.

    I have an xml document. Structure and size do not matter here.
    Important, it is UTF-8 encoded.

    So, what I do now is take that whole xmlDocument get the OuterXml of it, and do a base64encoding of the whole document.

    That base64 String is now being put into another xmldocument.

    Everything works fine, up till now,
    but what I do next is to decode that data again, select the base64 data from the document and save it again. That works to, but if I now open the latest xml Document it is UTF-16 encoded all the sudden.

    What am I doing wrong?

    Heres my Source for encoding the data:

    Code:
    using System;
    using System.Xml;
    using System.IO;
    using System.Text;
    
    namespace base64EncodeData
    {
    	/// <summary>
    	/// Summary description for Class1.
    	/// </summary>
    	class Class1
    	{
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			StringBuilder myBuilder = new StringBuilder();
    			myBuilder.Append("<?xml version='1.0'?>");
    			myBuilder.Append("<root>");
    			myBuilder.Append("<data>");
    			myBuilder.Append("</data>");
    			myBuilder.Append("</root>");
    			
    			StreamReader myReader = new StreamReader("test.xml");
    			string workingData = myReader.ReadToEnd();
    			myReader.Close();
    
    			byte[] encData_byte = new byte[workingData.Length];
    			encData_byte = System.Text.Encoding.Unicode.GetBytes(workingData);	
    			string encodedData = Convert.ToBase64String(encData_byte);
    
    			XmlDocument myDoc = new XmlDocument();
    			myDoc.LoadXml(myBuilder.ToString());
    			myDoc.DocumentElement.SelectSingleNode("//data").InnerText = encodedData;
    
    			StreamWriter myWriter = new StreamWriter(Guid.NewGuid().ToString() + ".xml");
    			myWriter.Write(myDoc.DocumentElement.OuterXml);
    			myWriter.Flush();
    			myWriter.Close();
    		}
    	}
    }
    Heres my Source for decoding the data:

    Code:
    using System;
    using System.Xml;
    using System.IO;
    
    namespace base64DecodeData
    {
    	/// <summary>
    	/// Summary description for Class1.
    	/// </summary>
    	class Class1
    	{
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			Console.WriteLine("Beginning of decoding");
    			Console.WriteLine("Load Document");
    			XmlDocument myDoc = new XmlDocument();
    			myDoc.Load(args[0]);
    			string data = myDoc.DocumentElement.SelectSingleNode("//data").InnerText.ToString();
    			Console.WriteLine("Decode Data");
    			System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();	
    			System.Text.Decoder utf8Decode = encoder.GetDecoder();
    			byte[] todecode_byte = Convert.FromBase64String(data);
    			int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);	
    			char[] decoded_char = new char[charCount];
    			utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);					
    			string result = new String(decoded_char);
    			Console.WriteLine("Save Document");
    			StreamWriter myWriter = new StreamWriter(Guid.NewGuid().ToString() + ".xml");
    			myWriter.Write(result);
    			myWriter.Flush();
    			myWriter.Close();
    			Console.WriteLine("End of Decoding");
    		}
    	}
    }
    Heres what my XmlDocument that I start with looks like in UltraEdit:
    Code:
    <?xml version="1.0"?>
    <root>
    	<testdata>alottadata</testdata>
    </root>
    And here it is after doing the encoding-decoding process

    Wheres my Error? Any help is appreciated
    Thanks,

    Stephan
    Attached Images Attached Images  
    Last edited by Sgt-Peppa; Jan 9th, 2004 at 05:02 AM.
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width