11using System ;
22using System . IO ;
33using System . Net ;
4+ using System . Web . Script . Serialization ;
45using System . Windows ;
56using System . Xml . Serialization ;
67using CodeDead . UpdateManager . Windows ;
@@ -18,17 +19,17 @@ public sealed class UpdateManager
1819 /// </summary>
1920 private readonly string _updateUrl ;
2021 /// <summary>
21- /// The Update object containing data about the current update
22- /// </summary>
23- private Update _update ;
24- /// <summary>
2522 /// The version of the application
2623 /// </summary>
2724 private readonly Version _applicationVersion ;
2825 /// <summary>
2926 /// The string variables that can be used to display information to the user
3027 /// </summary>
3128 private StringVariables _stringVariables ;
29+ /// <summary>
30+ /// The DataType that can be used to deserialize the update information
31+ /// </summary>
32+ private readonly DataType _dataType ;
3233 #endregion
3334
3435 /// <summary>
@@ -37,14 +38,12 @@ public sealed class UpdateManager
3738 /// <param name="version">Your application version</param>
3839 /// <param name="updateUrl">The URL where your XML update file is located</param>
3940 /// <param name="stringVariables">StringVariables object containing strings that can be used to display information to the user</param>
40- public UpdateManager ( Version version , string updateUrl , StringVariables stringVariables )
41+ /// <param name="datatype">The DataType that can be used to deserialize the update information</param>
42+ public UpdateManager ( Version version , string updateUrl , StringVariables stringVariables , DataType datatype )
4143 {
4244 _updateUrl = updateUrl ;
43-
44- _update = new Update ( ) ;
45+ _dataType = datatype ;
4546 _applicationVersion = new Version ( version . Major , version . Minor , version . Build , version . Revision ) ;
46-
47- _update . SetApplicationVersion ( _applicationVersion ) ;
4847 SetStringVariables ( stringVariables ) ;
4948 }
5049
@@ -53,14 +52,12 @@ public UpdateManager(Version version, string updateUrl, StringVariables stringVa
5352 /// </summary>
5453 /// <param name="version">Your application version</param>
5554 /// <param name="updateUrl">The URL where your XML update file is located</param>
56- public UpdateManager ( Version version , string updateUrl )
55+ /// <param name="dataType">The DataType that can be used to deserialize the update information</param>
56+ public UpdateManager ( Version version , string updateUrl , DataType dataType )
5757 {
5858 _updateUrl = updateUrl ;
59-
60- _update = new Update ( ) ;
59+ _dataType = dataType ;
6160 _applicationVersion = new Version ( version . Major , version . Minor , version . Build , version . Revision ) ;
62-
63- _update . SetApplicationVersion ( _applicationVersion ) ;
6461 SetStringVariables ( new StringVariables ( ) ) ;
6562 }
6663
@@ -73,31 +70,30 @@ public async void CheckForUpdate(bool showErrors, bool showNoUpdates)
7370 {
7471 try
7572 {
76- string xml = await new WebClient ( ) . DownloadStringTaskAsync ( _updateUrl ) ;
77-
78- XmlSerializer serializer = new XmlSerializer ( _update . GetType ( ) ) ;
79- using ( MemoryStream stream = new MemoryStream ( ) )
73+ string data = await new WebClient ( ) . DownloadStringTaskAsync ( _updateUrl ) ;
74+ Update update ;
75+ switch ( _dataType )
8076 {
81- StreamWriter writer = new StreamWriter ( stream ) ;
82- writer . Write ( xml ) ;
83- writer . Flush ( ) ;
84- stream . Position = 0 ;
85- _update = ( Update ) serializer . Deserialize ( stream ) ;
86- _update . SetApplicationVersion ( _applicationVersion ) ;
87- writer . Dispose ( ) ;
77+ default :
78+ update = DeserializeJson ( data ) ;
79+ break ;
80+ case DataType . Xml :
81+ update = DeserializeXml ( data ) ;
82+ break ;
8883 }
8984
90- if ( _update . CheckForUpdate ( ) )
85+
86+ if ( update . CheckForUpdate ( ) )
9187 {
9288 UpdateWindow window = new UpdateWindow
9389 {
9490 Title = _stringVariables . TitleText ,
95- InformationTextBlockContent = _update . UpdateInfo ,
91+ InformationTextBlockContent = update . UpdateInfo ,
9692 InformationButtonContent = _stringVariables . InformationButtonText ,
9793 CancelButtonContent = _stringVariables . CancelButtonText ,
9894 DownloadButtonContent = _stringVariables . DownloadButtonText ,
99- DownloadUrl = _update . UpdateUrl ,
100- InformationUrl = _update . InfoUrl ,
95+ DownloadUrl = update . UpdateUrl ,
96+ InformationUrl = update . InfoUrl ,
10197 UpdateNowText = _stringVariables . UpdateNowText
10298 } ;
10399 window . ShowDialog ( ) ;
@@ -119,6 +115,41 @@ public async void CheckForUpdate(bool showErrors, bool showNoUpdates)
119115 }
120116 }
121117
118+ /// <summary>
119+ /// Deserialize the XML data into an Update object
120+ /// </summary>
121+ /// <param name="data">The XML data that should be deserialized</param>
122+ /// <returns>The Update object that was deserialized</returns>
123+ private Update DeserializeXml ( string data )
124+ {
125+ Update update ;
126+ XmlSerializer serializer = new XmlSerializer ( typeof ( Update ) ) ;
127+ using ( MemoryStream stream = new MemoryStream ( ) )
128+ {
129+ StreamWriter writer = new StreamWriter ( stream ) ;
130+ writer . Write ( data ) ;
131+ writer . Flush ( ) ;
132+ stream . Position = 0 ;
133+ update = ( Update ) serializer . Deserialize ( stream ) ;
134+ update . SetApplicationVersion ( _applicationVersion ) ;
135+ writer . Dispose ( ) ;
136+ }
137+
138+ return update ;
139+ }
140+
141+ /// <summary>
142+ /// Deserialize the Json data into an Update object
143+ /// </summary>
144+ /// <param name="data">The Json data that should be deserialized</param>
145+ /// <returns>The Update object that was deserialized</returns>
146+ private Update DeserializeJson ( string data )
147+ {
148+ Update update = new JavaScriptSerializer ( ) . Deserialize < Update > ( data ) ;
149+ update . SetApplicationVersion ( _applicationVersion ) ;
150+ return update ;
151+ }
152+
122153 /// <summary>
123154 /// Change the StringVariables during runtime
124155 /// </summary>
0 commit comments