字节数组

扫码查看
本文介绍了字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在将一些字节数组写入XElement以保存在XML文件中。
在某些情况下,此字节数组可能包含一个小图像。


Hello,

I am writing some Byte arrays to an XElement to save on a XML file.
In some cases this byte array might contain a small image.

I am doing it as follows:

      XElement node = new XElement("Node",
        new XElement("ByteTest1", Convert.ToBase64String(new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })),
        new XElement("ByteTest2", new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })
      );
      Console.WriteLine(node);



我得到以下内容:

<节点>
  < ByteTest1> AAECAwQFBgcICQ ==< / ByteTest1>
  < ByteTest2> 0123456789< / ByteTest2>
< / Node>

我应该使用Convert.ToBase64String吗?在哪种情况下我不应该使用它?如果我应该总是使用它是否可能,或者有意义,创建一个XElement扩展,当值为Byte []类型时,它使用Base64String?

谢谢,
Miguel


I get the following:

<Node>
  <ByteTest1>AAECAwQFBgcICQ==</ByteTest1>
  <ByteTest2>0123456789</ByteTest2>
</Node>

Should I use Convert.ToBase64String? In which case shouldn't I use it?
And if I should always use it is it possible, or it makes sense, to create a XElement extension that when the value is of type Byte[] it uses Base64String?

Thanks,
Miguel

推荐答案

public class XBase64Element : XElement
{
    public XBase64Element(XName name, byte[] data)
        : base(name, Convert.ToBase64String(data))
    {
    }
}




然后在您的示例中,您只需使用新的XBase64Element替换新的XElement。请注意,在消费者方面这样做并不是那么容易。

谢谢,



Then in your sample you just replace the new XElement with new XBase64Element. Note that doing this on the consumer side would not be that easy though.

Thanks,


这篇关于字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 00:41
查看更多