Bonjour, j'éssaye de deserialiser des messages XML qui peuvent contenir ou non un namespace. (reçus d'une application externe)
J'utilise une classe généré à partir d'un XSD qui contient déjà un namespace.
Je peux serialiser les XML avec namespace. Mais pas les autres.
-Est ce que je peux ajouter des options surt mon XmlSerialiser pour qu'il traite les deux types de XML sans apporter des modification à ma classe MyCommand.
Je vous remercie pour votre aide
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 public class XMLTest { public void testXml() { //<MyCommand Version="2"><Comment>ttt</Comment></MyCommand> string commandWithNullNS = "<MyCommand Version=\"2\"><Comment>ttt</Comment></MyCommand>"; string commandWithNS = "<MyCommand xmlns=\"http://www.mycompany.com/myproject\" Version=\"2\"><Comment>ttt</Comment></MyCommand>"; try { //Sans Exception MyCommand resultWithNS = DeserializeObject<MyCommand>(commandWithNS); //Exception : <MyCommand xmlns=''> n'était pas attendu. MyCommand resultwithNullNs = DeserializeObject<MyCommand>(commandWithNullNS); } catch (Exception e) { Console.WriteLine(e.Message); } } public static T DeserializeObject<T>(string objectData) { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T)); T result; using (System.IO.TextReader reader = new System.IO.StringReader(objectData)) { result = (T)xs.Deserialize(reader); } return result; } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.mycompany.com/myproject")] [System.Xml.Serialization.XmlRootAttribute("MyCommand", Namespace = "http://www.mycompany.com/myproject", IsNullable = false)] public partial class MyCommand { private string commentField; private string versionField; /// <remarks/> public string Comment { get { return this.commentField; } set { this.commentField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute(DataType = "integer")] public string Version { get { return this.versionField; } set { this.versionField = value; } } }
Partager