如何在xml字符串中发送二进制数据

时间:2010-05-31 03:41:38

标签: c# binary filestream

我想以下面的xml格式将二进制文件发送到.net c#组件

<BinaryFileString fileType='pdf'>
    <!--binary file data string here-->
</BinaryFileString>

在被调用的组件中,我将使用上面的xml字符串,并将BinaryFileString标记中接收的二进制字符串转换为filetype =''属性指定的文件。文件类型可以是doc / pdf / xls / rtf

我在调用应用程序中有代码从要发送的文件中取出字节。如何准备将其与包裹在其周围的xml标签一起发送?我希望应用程序向组件发送一个字符串而不是字节流。这是因为我无法通过查看字节流来解密文件类型[pdf / doc / xls]。因此具有filetype属性的xml字符串。关于这个的任何想法?

提取下面的字节的方法

   FileStream fs = new FileStream(_filePath, FileMode.Open, FileAccess.Read);
   using (Stream input = fs)
   {
      byte[] buffer = new byte[8192];
      int bytesRead;
      while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
      {}
   }
   return buffer;

感谢。

编辑:

只是为了澄清为什么我使用xml字符串而不是在我的组件上设置属性。实际上我的调用应用程序试图模拟Siebel如何调用我的组件。 http://download.oracle.com/docs/cd/E05553_01/books/eScript/eScript_JSReference244.html#wp1014380 我不确定Siebel是否可以根据需要设置我的组件属性。所以我正在研究它在xml中发送数据的角度。

4 个答案:

答案 0 :(得分:7)

Base64表示通常用于表示二进制数据。

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 XML file.
    System.IO.StreamWriter outFile; 
    try {
        outFile = new System.IO.StreamWriter(outputFileName,
                                    false,
                                    System.Text.Encoding.ASCII);             
        outFile.Write("<BinaryFileString fileType='pdf'>");
        outFile.Write(base64String);
        outFile.Write("</BinaryFileString>");
        outFile.Close();
    }
    catch (System.Exception exp) {
        // Error creating stream or writing to it.
        System.Console.WriteLine("{0}", exp.Message);
    }
}

在接收端,您可以撤消此操作并获取原始文件内容,如下所述。

        // 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;
        }

答案 1 :(得分:4)

你可以BASE64你的字节吗? MSDN参考:Convert.ToBase64Convert.FromBase64String

答案 2 :(得分:3)

扩展@russau's回答它会像这样工作:

var s = "Hello World";
var b = Encoding.Default.GetBytes(s);
var bstr = Convert.ToBase64String(b);
Console.WriteLine("Original String:" + s);
Console.WriteLine("Base64 String:" + bstr);

var fromBStr = Convert.FromBase64String(bstr);
var st = Encoding.Default.GetString(fromBStr);
Console.WriteLine("Converted string: " + st);

你不需要前两行:

var s = "Hello World";
var b = Encoding.Default.GetBytes(s);

因为你已经有了一个字节数组。我使用字符串来表示当你从Convert.FromBase64String

转换字节数组时,你得到的结果完全相同

答案 3 :(得分:0)

您提到您正在调用C#组件。我不确定为什么使用这个组件意味着你需要创建一个XML字符串。

是否可以定义用于保存数据的类而不是使用XML字符串? e.g。

public enum FileType
{
    Word,
    Excel,
    RichText,
    PDF
}

public class FileData
{
    public FileType TypeOfFile
    {
        get;
        set;
    }

    public byte[] Data
    {
        get;
        set;
    }
}

然后组件的调用者只设置FileType和byte []。然后将更明确地定义组件的接口(FileData类,而不是更通用的字符串或XmlDocument)。

相关问题