如何将序列化的字节数组转换回其文本形式

本文关键字:转换 文本 数组 字节数 序列化 字节 | 更新日期: 2023-09-27 18:27:13

我有一个文本,它是一个对象的属性。对象获得XmlSerialized,之后在XML调用Text中有一个元素表示对象中的文本。我想知道如何把它变成绳子。

串行化的类型XmlSerializer serizlizer = new XmlSerializer(typeof(Act));

类中的属性

[System.Runtime.Serialization.OptionalFieldAttribute()]
private byte[] ActTextField;

在xml文件中,它看起来像这样:

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAALQAAAAAAAAAAEAAALwAAAAEAAAD+////AAAAACwAAAD////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////spcEAJ2AJBAAA8BK/AAAAAAAAEAAAAAAABgAAYB4AAA4AYmpiavbg9uAAAAAAAAAAAAAAAAAAAAAAAAACBBYALiIAAJSKAQCUigEAzwYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//w8AAAAAAAAAAAD//w8AAAAAAAAAAAD//w8AAAAAAAAAAAAAAAAAAAAAAKQAAAAAANADAAAAAAAA0AMAANADAAAAAAAA0AMAAAAAAADQAwAAAAAAANADAAAAAAAA0AMAABQAAAAAAAAAAAAAAOQDAAAAAAAArAgAAAAAAACsCAAAAAAAAKwIAAAAAAAArAgAABQAAADACAAAFAAAAOQDAAAAAAAA/Q4AALYAAADgCAAAAAAAAOAIAAAAAAAA4AgAAAAAAADgCAAAAAAAAOAIAAAAAAAA4AgAAAAAAADgCAAAAAAAAOAIAAAAAAAAWA4AAAIAAABaDgAAAAAAAFoOAAAAAAAAWg4AAAAAAABaDgAAAAAAAFoOAAAAAAAAWg4AACQAAACzDwAAaAIAABsSAACSAAAAfg4AADkAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB4EIAAgAB8EIAAgACAEIAAgABUEIAAgABQEIAAgABUEIAAgABsEIAAgABUEIAAgAB0EIAAgABgEIAAgABUEIAANABUELgAgAEAEMAQ5BD4EPQQ1BD0EIABBBEoENAQgADIEIAA3BDAEOgRABDgEQgQ+BCAAQQRKBDQENQQxBD0EPgQgADcEMARBBDUENAQwBD0EOAQ1BCAAPQQwBCAANAQyBDAENAQ1BEEENQRCB

我甚至无法想象它的编码是什么以及如何解码。我试图将它读取到字节数组中,但在应用了一些解码后,它实际上没有工作Encode.UTF8、Encode.ASCII、

如何将序列化的字节数组转换回其文本形式

对我来说,这看起来像Base64-只需使用

byte[] data = Convert.FromBase64String(base64Text);

奇怪的是,如果这真的是一个text属性,它会使用base64。我希望只是文本。

要将二进制数据转换回文本,您需要知道使用了哪种编码将其转换为二进制数据,而UTF-8是最有可能的,但老实说,其中所有重复的AAAAA...部分使其看起来与文本非常不同。

EDIT:现在我们已经看到了字段声明,我们可以看到它一开始是一个byte[],所以以这种方式对它进行编码是有意义的。从评论来看,这听起来像是一个Word文件——在这一点上提取文本是一个非常独立的问题。