【问题标题】:how to send binary data within an xml string如何在 xml 字符串中发送二进制数据
【发布时间】:2023-03-12 01:27:01
【问题描述】:

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

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

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

我在调用应用程序中有代码从要发送的文件中取出字节。我如何准备将其与包裹在其周围的 xml 标签一起发送?我希望应用程序向组件发送一个字符串而不是字节流。这是因为我无法仅通过查看字节流来破译文件类型 [pdf/doc/xls]。因此具有文件类型属性的 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 发送数据的角度。

【问题讨论】:

  • 为什么需要将二进制数据包装在 xml 中?

标签: c# binary filestream


【解决方案1】:

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

【讨论】:

  • 小问题:UUEncoding 和 Base64 是两个不同的东西。 en.wikipedia.org/wiki/Uuencodeden.wikipedia.org/wiki/Base64
  • @russau 是的,但像 Convert.ToBase64String 这样的文档没有帮助(查看备注部分;-)。
  • 我只注意到说 Base64 是一种“通用”方法是不准确的。我目前在使用嵌入式 TIF 创建 XML 时遇到了一些有趣的问题。它需要模仿我无法访问其源代码的供应商的另一个接口。然而,他们确实将二进制文件撞到了 XML 中——根本不是 Base64 编码的。我在其他行业的其他情况下也看到了这一点。这可能是最聪明的方法,但远非普遍。
【解决方案2】:

你能对你的字节进行 BASE64 处理吗? MSDN 参考:Convert.ToBase64Convert.FromBase64String

【讨论】:

  • 如何转换回来?一旦转换为字符串,这些字节就不能用来重新创建文件了,对吧?
  • Convert.FromBase64String 将字符串转换回字节数组
  • 谢谢!我计划很快使用 Cryptography.FromBase64Transform,因为我不知道这些更简单的方法。
  • 感谢 Russau - 会试试这个。
【解决方案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 转换字节数组时,您最终得到的值与开始时完全相同。

【讨论】:

    【解决方案4】:

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

    是否可以定义类来保存数据而不是使用 XML 字符串?例如

    public enum FileType
    {
        Word,
        Excel,
        RichText,
        PDF
    }
    
    public class FileData
    {
        public FileType TypeOfFile
        {
            get;
            set;
        }
    
        public byte[] Data
        {
            get;
            set;
        }
    }
    

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

    【讨论】:

    • 谢谢图佐。实际上,我的调用应用程序正在尝试模拟 Siebel 将如何调用我的组件。 download.oracle.com/docs/cd/E05553_01/books/eScript/…我不确定它是否可以根据需要设置我的组件属性。所以我正在研究它在 xml 中发送数据的角度。
    猜你喜欢
    • 2013-02-28
    • 2019-07-18
    • 2013-07-07
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 2014-04-09
    相关资源
    最近更新 更多