1 using System;
2 using System.IO;
3 using System.Collections.Generic;
4 using System.Text;
5 using System.Xml;
6 using System.Xml.Serialization;
7
8 namespace org.xmlBlaster.util {
9 public static class Serialization {
10
11 public static type Deserialize<type>(byte[] data) {
12 XmlSerializer xs = new XmlSerializer(typeof(type));
13 MemoryStream memoryStream = new MemoryStream(data);
14 /*
15 XmlWriterSettings settings = new XmlWriterSettings();
16 //settings.OmitXmlDeclaration = true;
17 //settings.ConformanceLevel = ConformanceLevel.Fragment;
18 //settings.CloseOutput = false;
19 //settings.OmitXmlDeclaration = true;
20 settings.Encoding = Encoding.UTF8;
21 XmlTextWriter xmlTextWriter = XmlWriter.Create(memoryStream, settings);
22 //XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
23 */
24 return (type)xs.Deserialize(memoryStream);
25 }
26
27 /// <summary>
28 /// Parse UTF-8 xml string
29 /// </summary>
30 /// <typeparam name="type"></typeparam>
31 /// <param name="data"></param>
32 /// <returns></returns>
33 public static type DeserializeStr<type>(string data) {
34 XmlSerializer xs = new XmlSerializer(typeof(type));
35 MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(data));
36 //XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
37 return (type)xs.Deserialize(memoryStream);
38 /*
39 XmlSerializer ser = new XmlSerializer(typeof(type), "");
40 return (type)ser.Deserialize(new StringReader(data));
41 */
42 }
43
44 /// <summary>
45 /// Returns the UTF-8 XML representation
46 /// </summary>
47 /// <typeparam name="type"></typeparam>
48 /// <param name="data"></param>
49 /// <returns></returns>
50 public static byte[] Serialize<type>(type data) {
51 MemoryStream memoryStream = new MemoryStream();
52 XmlSerializer xs = new XmlSerializer(data.GetType());
53 XmlWriterSettings settings = new XmlWriterSettings();
54 settings.OmitXmlDeclaration = true;
55 //settings.ConformanceLevel = ConformanceLevel.Fragment;
56 //settings.CloseOutput = false;
57 // Correct BOM is:
58 // X'FEFF' big endian UTF-16
59 // X'FFFE' little endian UTF-16
60 // X'EFBBBF' UTF-8
61 // Windows CE: return X'FEFF' for UTF-8 which is wrong
62 // Win32: behaves correctly to X'EFBBBF' for UTF-8
63 settings.Encoding = Encoding.UTF8;
64 XmlWriter xmlWriter = XmlWriter.Create(memoryStream, settings);
65 xs.Serialize(xmlWriter, data);
66 //memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
67 byte[] bytes = memoryStream.ToArray();
68 if (bytes[0] == 0XEF && bytes[1] == 0XBB && bytes[2] == 0XBF) {
69 int len = bytes.Length;
70 byte[] tmp = new byte[len - 3];
71 //System.Array.Copy(bytes, 3, tmp, 0, tmp.Length);
72 for (int i = 3; i < len; i++)
73 tmp[i - 3] = bytes[i];
74 return tmp;
75 }
76 else if (bytes[0] == 0XFE && bytes[1] == 0XFF ||
77 bytes[0] == 0XFF && bytes[1] == 0XFE) {
78 int len = bytes.Length;
79 byte[] tmp = new byte[len - 2];
80 for (int i = 2; i < len; i++)
81 tmp[i - 2] = bytes[i];
82 return tmp;
83 }
84 return bytes;
85 }
86
87 public static string SerializeStr<type>(type data) {
88 MemoryStream memoryStream = new MemoryStream();
89 XmlSerializer xs = new XmlSerializer(data.GetType(), "");
90 XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
91 xs.Serialize(xmlTextWriter, data);
92 memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
93 string xml = UTF8ByteArrayToString(memoryStream.ToArray());
94 return xml;
95 /*
96 StringWriter wr = new StringWriter();
97 XmlSerializer ser = new XmlSerializer(data.GetType(), "");
98 ser.Serialize(wr, data);
99 return wr.ToString();
100 */
101 }
102
103 /**
104 * Convert a UTF-8 byte array to a UTF-16 string
105 * @param dBytes UTF-8 multibyte string from C (zero terminated)
106 * @return string to be used in C#
107 */
108 public static string UTF8ByteArrayToString(byte[] dBytes) {
109 string str;
110 str = System.Text.Encoding.UTF8.GetString(dBytes, 0, dBytes.Length);
111 return str;
112 }
113 /*
114 public static String UTF8ByteArrayToString(Byte[] characters) {
115 UTF8Encoding encoding = new UTF8Encoding();
116 String constructedString = encoding.GetString(characters); // not for CF 2?
117 return (constructedString);
118 }
119 */
120 public static Byte[] StringToUTF8ByteArray(String pXmlString) {
121 UTF8Encoding encoding = new UTF8Encoding();
122 Byte[] byteArray = encoding.GetBytes(pXmlString);
123 return byteArray;
124 }
125
126 }
127 }
syntax highlighted by Code2HTML, v. 0.9.1