J'ai un objet que je dois sérialiser en XML. Pour les besoins demandés je suis obligé de le fournir en XML, et non en binaire.

Ma classe contient deux proprités:
- un string
- un élément qui va contenir un élement sérialiser en binaire

J'arrive bien à sérialiser mon objet. Mais la déserialisation plante à cause des caractères spéciaux.

Comment faire pour que ça ne plante pas ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
 
namespace MonProg
{
	class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{
			Hashtable hash = new Hashtable();
 
			hash.Add("Test1", "Val1");
			hash.Add("Test2", "Val2");
 
			//Serialisation binaire de ma hashtable
			MemoryStream stream = new MemoryStream();
			BinaryFormatter bf = new BinaryFormatter();
			bf.Serialize(stream,hash);
 
			stream.Position = 0;
			string data = new StreamReader(stream).ReadToEnd();
 
			Exemple ex = new Exemple("coucou", data);
 
			//Serialisation
			string xml = ex.GetXml();
 
			//Deserialisation (plantage)
			Exemple ex2 = Exemple.FromXml(xml);
 
			Console.ReadLine();
		}
	}
 
	public class Exemple
	{
		private string _s;
		private string _h;
 
		public Exemple()
		{
		}
 
		public Exemple(string s, string h)
		{
			_s = s;
			_h = h;
		}
 
		public string GetXml()
		{
			MemoryStream stream = new MemoryStream();
			XmlTextWriter flow = new XmlTextWriter(stream, Encoding.UTF8);
 
			XmlSerializer serialiseur = new XmlSerializer(typeof(Exemple));
 
			serialiseur.Serialize(flow,this);
 
			stream.Position = 0;
			string xml = new StreamReader(stream).ReadToEnd();
			flow.Close();
 
			return xml;
		}
 
		public static Exemple FromXml(string xml)
		{			
			XmlSerializer serialiseur = new XmlSerializer(typeof(Exemple));
 
			MemoryStream stream = new MemoryStream();
 
			StringReader strReader = new StringReader(xml);
			Exemple tmp = (Exemple)serialiseur.Deserialize(strReader);	
 
			return tmp;	
		}
 
		public string S
		{
			get{ return _s;}
			set { _s = value;}
		}
 
		public string H
		{
			get{ return _h;}
			set { _h = value;}
		}
	}
}