SHOW:
|
|
- or go back to the newest paste.
1 | using System.Collections.Generic; | |
2 | using System.Diagnostics; | |
3 | using System.Globalization; | |
4 | using System.IO; | |
5 | using System.Runtime.Serialization; | |
6 | using System.Xml; | |
7 | using System.Xml.Serialization; | |
8 | using NUnit.Framework; | |
9 | ||
10 | namespace UnitTests | |
11 | { | |
12 | [TestFixture] | |
13 | public class SoTest | |
14 | { | |
15 | #region Test | |
16 | ||
17 | [Test] | |
18 | public void TestDeserializing() | |
19 | { | |
20 | const string xml = @"<root> | |
21 | <company> | |
22 | <location>USA</location> | |
23 | <name>TopCars</name> | |
24 | </company> | |
25 | <CarList name=""CarCounts""> | |
26 | <ModelList name=""Models""> | |
27 | <Column name=""Ford"">50</Column> | |
28 | <Column name=""Chevy"">65</Column> | |
29 | <Column name=""Dodge"">75</Column> | |
30 | </ModelList> | |
31 | </CarList> | |
32 | </root>"; | |
33 | ||
34 | var deserializer = new XmlSerializer(typeof(Root)); | |
35 | ||
36 | Root root; | |
37 | using (var stringReader = new StringReader(xml)) | |
38 | using (var xmlReader = XmlReader.Create(stringReader)) | |
39 | { | |
40 | root = (Root) deserializer.Deserialize(xmlReader); | |
41 | } | |
42 | var models = new Models(root.CarList.Models); | |
43 | ||
44 | Debug.Print(models.Chevy.ToString(CultureInfo.InvariantCulture)); | |
45 | Debug.Print(models.Ford.ToString(CultureInfo.InvariantCulture)); | |
46 | } | |
47 | ||
48 | #endregion | |
49 | ||
50 | #region Shim Class | |
51 | ||
52 | class Models | |
53 | { | |
54 | private readonly List<Model> _models; | |
55 | ||
56 | public Models(List<Model> models) | |
57 | { | |
58 | this._models = models; | |
59 | } | |
60 | ||
61 | public int Chevy | |
62 | { | |
63 | get | |
64 | { | |
65 | var chevy = this._models.Find(model => model.Name == "Chevy"); | |
66 | if(chevy == null) | |
67 | { | |
68 | throw new SerializationException("Xml must contain a model called Chevy"); | |
69 | } | |
70 | return chevy.Value; | |
71 | } | |
72 | } | |
73 | ||
74 | public int Ford | |
75 | { | |
76 | get | |
77 | { | |
78 | var ford = this._models.Find(model => model.Name == "Ford"); | |
79 | if (ford == null) | |
80 | { | |
81 | throw new SerializationException("Xml must contain a model called Ford"); | |
82 | } | |
83 | return ford.Value; | |
84 | } | |
85 | } | |
86 | ||
87 | // etc. | |
88 | } | |
89 | ||
90 | #endregion | |
91 | ||
92 | #region Deserializing Classes | |
93 | ||
94 | [XmlRoot("root")] | |
95 | public class Root | |
96 | { | |
97 | [XmlElement("company")] | |
98 | public Company Company { get; set; } | |
99 | ||
100 | [XmlElement("CarList")] | |
101 | public CarList CarList { get; set; } | |
102 | } | |
103 | ||
104 | ||
105 | public class Company | |
106 | { | |
107 | [XmlElement("location")] | |
108 | public string Location { get; set; } | |
109 | ||
110 | [XmlElement("name")] | |
111 | public string Name { get; set; } | |
112 | } | |
113 | ||
114 | public class CarList | |
115 | { | |
116 | [XmlArray("ModelList")] | |
117 | [XmlArrayItem("Column")] | |
118 | public List<Model> Models { get; set; } | |
119 | } | |
120 | ||
121 | public class Model | |
122 | { | |
123 | [XmlAttribute("name")] | |
124 | public string Name { get; set; } | |
125 | ||
126 | [XmlText] | |
127 | public int Value { get; set; } | |
128 | } | |
129 | ||
130 | #endregion | |
131 | } | |
132 | } |