C# 手动反序列化
C# Manually Deserialization
我想知道是否可以手动解析已在 c# 中序列化的序列化二进制文件。
我的最终目标是解析已在 c# 中序列化的多维数组的序列化二进制文件,并在 java、
中进行解析
我想知道是否有任何 algorithm/cheat-sheet 可以帮助我理解序列化二进制文件的结构?
任何 pointers/hints 非常感谢。
注意:我不想反序列化 Java 中的序列化对象,我想知道二进制序列化对象的结构,以便我可以按照我想要的方式解析它。
正如所建议的那样,XML 可以成为很好的工具,这里有一个小的演示示例:
C# 序列化:
// NEEDED IMPORTS
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
static void Main(string[] args)
{
// build a list of lists from 0 to 99 divided on
// 10 inner list each with 10 elements
List<List<string>> list = new List<List<string>>();
for(int i=0; i<10; i++)
{
list.Add(new List<string>());
for (int j = i * 10; j < i * 10 + 10; j++)
list[i].Add(j.ToString());
}
// serialize to xml
XmlSerializer ser = new XmlSerializer(list.GetType());
TextWriter writer = new StreamWriter("serialized.xml");
ser.Serialize(writer, list);
}
示例输出:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfString>
<string>0</string>
<string>1</string>
<string>2</string>
<string>3</string>
<string>4</string>
<string>5</string>
<string>6</string>
<string>7</string>
<string>8</string>
<string>9</string>
</ArrayOfString>
...
基于序列化XML,Java反序列化:
// NEEDED IMPORTS
import java.io.*;
import java.util.*;
import org.jdom2.*;
import org.jdom2.input.*;
public static void main(String[] args) throws JDOMException, IOException {
// build DOM from the XML file - generic for all XML files
File fXmlFile = new File("serialized.xml"); // file we created in C#
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(fXmlFile);
Element root = document.getRootElement();
// build List<List<String>> using expected format
if(!root.getName().equals("ArrayOfArrayOfString")){
System.out.println("invalid root element");
return;
}
List<List<String>> list = new ArrayList<>();
List<Element> children = root.getChildren();
for(int i = 0; i<children.size(); i++){
Element child = children.get(i);
if(child.getName().equals("ArrayOfString")){
List<String> innerList = new ArrayList<>();
list.add(innerList);
List<Element> innerChildren = child.getChildren();
for(int j=0; j < innerChildren.size(); j++){
Element elem = innerChildren.get(j);
if(elem.getName().equals("string"))
innerList.add(elem.getValue());
}
}
}
for(int i = 0; i<list.size(); i++){
System.out.print(String.format("InnerList[%d]: ", i));
List<String> innerList = list.get(i);
for(int j=0; j<innerList.size(); j++)
System.out.print(String.format("\"%s\" ",innerList.get(j)));
System.out.println();
}
}
输出:
InnerList[0]: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
InnerList[1]: "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
InnerList[2]: "20" "21" "22" "23" "24" "25" "26" "27" "28" "29"
InnerList[3]: "30" "31" "32" "33" "34" "35" "36" "37" "38" "39"
InnerList[4]: "40" "41" "42" "43" "44" "45" "46" "47" "48" "49"
InnerList[5]: "50" "51" "52" "53" "54" "55" "56" "57" "58" "59"
InnerList[6]: "60" "61" "62" "63" "64" "65" "66" "67" "68" "69"
InnerList[7]: "70" "71" "72" "73" "74" "75" "76" "77" "78" "79"
InnerList[8]: "80" "81" "82" "83" "84" "85" "86" "87" "88" "89"
InnerList[9]: "90" "91" "92" "93" "94" "95" "96" "97" "98" "99"
这是一个非常简单的演示,展示了使用 XML 可以做什么。我没有在这段代码中进行错误处理。
编辑:Java DOM 构建器并非直接来自 JDK。您需要从他们的网站下载它:http://www.jdom.org/downloads/ 并将 jre
文件导入您的 Java 项目。我还包含了此操作所需的 import
语句。
我想知道是否可以手动解析已在 c# 中序列化的序列化二进制文件。
我的最终目标是解析已在 c# 中序列化的多维数组的序列化二进制文件,并在 java、
中进行解析我想知道是否有任何 algorithm/cheat-sheet 可以帮助我理解序列化二进制文件的结构?
任何 pointers/hints 非常感谢。
注意:我不想反序列化 Java 中的序列化对象,我想知道二进制序列化对象的结构,以便我可以按照我想要的方式解析它。
正如所建议的那样,XML 可以成为很好的工具,这里有一个小的演示示例:
C# 序列化:
// NEEDED IMPORTS
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
static void Main(string[] args)
{
// build a list of lists from 0 to 99 divided on
// 10 inner list each with 10 elements
List<List<string>> list = new List<List<string>>();
for(int i=0; i<10; i++)
{
list.Add(new List<string>());
for (int j = i * 10; j < i * 10 + 10; j++)
list[i].Add(j.ToString());
}
// serialize to xml
XmlSerializer ser = new XmlSerializer(list.GetType());
TextWriter writer = new StreamWriter("serialized.xml");
ser.Serialize(writer, list);
}
示例输出:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfString>
<string>0</string>
<string>1</string>
<string>2</string>
<string>3</string>
<string>4</string>
<string>5</string>
<string>6</string>
<string>7</string>
<string>8</string>
<string>9</string>
</ArrayOfString>
...
基于序列化XML,Java反序列化:
// NEEDED IMPORTS
import java.io.*;
import java.util.*;
import org.jdom2.*;
import org.jdom2.input.*;
public static void main(String[] args) throws JDOMException, IOException {
// build DOM from the XML file - generic for all XML files
File fXmlFile = new File("serialized.xml"); // file we created in C#
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(fXmlFile);
Element root = document.getRootElement();
// build List<List<String>> using expected format
if(!root.getName().equals("ArrayOfArrayOfString")){
System.out.println("invalid root element");
return;
}
List<List<String>> list = new ArrayList<>();
List<Element> children = root.getChildren();
for(int i = 0; i<children.size(); i++){
Element child = children.get(i);
if(child.getName().equals("ArrayOfString")){
List<String> innerList = new ArrayList<>();
list.add(innerList);
List<Element> innerChildren = child.getChildren();
for(int j=0; j < innerChildren.size(); j++){
Element elem = innerChildren.get(j);
if(elem.getName().equals("string"))
innerList.add(elem.getValue());
}
}
}
for(int i = 0; i<list.size(); i++){
System.out.print(String.format("InnerList[%d]: ", i));
List<String> innerList = list.get(i);
for(int j=0; j<innerList.size(); j++)
System.out.print(String.format("\"%s\" ",innerList.get(j)));
System.out.println();
}
}
输出:
InnerList[0]: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
InnerList[1]: "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
InnerList[2]: "20" "21" "22" "23" "24" "25" "26" "27" "28" "29"
InnerList[3]: "30" "31" "32" "33" "34" "35" "36" "37" "38" "39"
InnerList[4]: "40" "41" "42" "43" "44" "45" "46" "47" "48" "49"
InnerList[5]: "50" "51" "52" "53" "54" "55" "56" "57" "58" "59"
InnerList[6]: "60" "61" "62" "63" "64" "65" "66" "67" "68" "69"
InnerList[7]: "70" "71" "72" "73" "74" "75" "76" "77" "78" "79"
InnerList[8]: "80" "81" "82" "83" "84" "85" "86" "87" "88" "89"
InnerList[9]: "90" "91" "92" "93" "94" "95" "96" "97" "98" "99"
这是一个非常简单的演示,展示了使用 XML 可以做什么。我没有在这段代码中进行错误处理。
编辑:Java DOM 构建器并非直接来自 JDK。您需要从他们的网站下载它:http://www.jdom.org/downloads/ 并将 jre
文件导入您的 Java 项目。我还包含了此操作所需的 import
语句。