请问c#中如何将byte[]保存到xml文件中?存入后如何取出还原?谢谢! 如题,^_^ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你是不是要把图片保存进XML文件啊,如果是给你个源码http://download.csdn.net/source/617737 干吗非要放到xml中啊,byte有好多不可显示的字符.直接用2进制流写到文件中即可.public static void WriteAllBytes( string path, byte[] bytes)public static byte[] ReadAllBytes( string path) 参考一下http://www.zzchn.com/edu/20080220/80290.shtml 先做encode to base64,变成可显示字符,保存。读出后,再uncode base64.使用System.Convert中的函数public static string ToBase64String( byte[] inArray)public static byte[] FromBase64String( string s)public void EncodeWithString() { System.IO.FileStream inFile; byte[] binaryData; try { inFile = new System.IO.FileStream(inputFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); binaryData = new Byte[inFile.Length]; long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length); inFile.Close(); } catch (System.Exception exp) { // Error creating stream or reading from it. System.Console.WriteLine("{0}", exp.Message); return; } // Convert the binary input into Base64 UUEncoded output. string base64String; try { base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length); } catch (System.ArgumentNullException) { System.Console.WriteLine("Binary data array is null."); return; } // Write the UUEncoded version to the output file. System.IO.StreamWriter outFile; try { outFile = new System.IO.StreamWriter(outputFileName, false, System.Text.Encoding.ASCII); outFile.Write(base64String); outFile.Close(); } catch (System.Exception exp) { // Error creating stream or writing to it. System.Console.WriteLine("{0}", exp.Message); } }public void DecodeWithString() { System.IO.StreamReader inFile; string base64String; try { char[] base64CharArray; inFile = new System.IO.StreamReader(inputFileName, System.Text.Encoding.ASCII); base64CharArray = new char[inFile.BaseStream.Length]; inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length); base64String = new string(base64CharArray); } catch (System.Exception exp) { // Error creating stream or reading from it. System.Console.WriteLine("{0}", exp.Message); return; } // Convert the Base64 UUEncoded input into binary output. byte[] binaryData; try { binaryData = System.Convert.FromBase64String(base64String); } catch (System.ArgumentNullException) { System.Console.WriteLine("Base 64 string is null."); return; } catch (System.FormatException) { System.Console.WriteLine("Base 64 string length is not " + "4 or is not an even multiple of 4." ); return; } // Write out the decoded data. System.IO.FileStream outFile; try { outFile = new System.IO.FileStream(outputFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write); outFile.Write(binaryData, 0, binaryData.Length); outFile.Close(); } catch (System.Exception exp) { // Error creating stream or writing to it. System.Console.WriteLine("{0}", exp.Message); } } 把byte[]转为base64string保存为文本格式就可以了 winform中如何根据窗体大小改变控件大小 有哪位高手有这样的控件吗 C#如何读取PDF文件呢 想实现异地监控,请给个思路 求教数据绑定之后为什么不能显示数据 短消息网关系统接口协议(SGIP) 100分:如何编程设置Excel中某个单元格中的属性(小数位几位) 物理的DataTable对象是不能单独存在的,它必须是DataSet的一部分。 怎么向一个C#程序发送标准的windows消息,UP有分 (追加100分!)在c#中如何指定axMediaPlayer播放文件的相对路径名?急! 如何取得word文档内容里的标题 WinForm 实现注册/卸载Windows服务
http://download.csdn.net/source/617737
直接用2进制流写到文件中即可.public static void WriteAllBytes(
string path,
byte[] bytes
)
public static byte[] ReadAllBytes(
string path
)
http://www.zzchn.com/edu/20080220/80290.shtml
读出后,再uncode base64.
使用System.Convert中的函数
public static string ToBase64String( byte[] inArray)
public static byte[] FromBase64String( string s)public void EncodeWithString() {
System.IO.FileStream inFile;
byte[] binaryData; try {
inFile = new System.IO.FileStream(inputFileName,
System.IO.FileMode.Open,
System.IO.FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0,
(int)inFile.Length);
inFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message);
return;
} // Convert the binary input into Base64 UUEncoded output.
string base64String;
try {
base64String =
System.Convert.ToBase64String(binaryData,
0,
binaryData.Length);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("Binary data array is null.");
return;
} // Write the UUEncoded version to the output file.
System.IO.StreamWriter outFile;
try {
outFile = new System.IO.StreamWriter(outputFileName,
false,
System.Text.Encoding.ASCII);
outFile.Write(base64String);
outFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message);
}
}
public void DecodeWithString() {
System.IO.StreamReader inFile;
string base64String; try {
char[] base64CharArray;
inFile = new System.IO.StreamReader(inputFileName,
System.Text.Encoding.ASCII);
base64CharArray = new char[inFile.BaseStream.Length];
inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
base64String = new string(base64CharArray);
}
catch (System.Exception exp) {
// Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message);
return;
} // Convert the Base64 UUEncoded input into binary output.
byte[] binaryData;
try {
binaryData =
System.Convert.FromBase64String(base64String);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("Base 64 string is null.");
return;
}
catch (System.FormatException) {
System.Console.WriteLine("Base 64 string length is not " +
"4 or is not an even multiple of 4." );
return;
} // Write out the decoded data.
System.IO.FileStream outFile;
try {
outFile = new System.IO.FileStream(outputFileName,
System.IO.FileMode.Create,
System.IO.FileAccess.Write);
outFile.Write(binaryData, 0, binaryData.Length);
outFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message);
}
}