/// <summary> /// Dequeue the first record /// </summary> public static RequestRecord DequeueRequestRecord() { List<RequestRecord> requests = new List<RequestRecord>(); DataContractJsonSerializer dc = new DataContractJsonSerializer(requests.GetType()); using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) { lock (fileLock) { using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"RequestRecords.xml", FileMode.Open, file)) { try { // if the file opens, read the contents requests = dc.ReadObject(stream) as List<RequestRecord>; if (requests.Count > 0) { RequestRecord record = requests[0]; requests.Remove(record); // remove the first entry stream.SetLength(0); stream.Position = 0; dc.WriteObject(stream, requests); record.DeserializeBody(); return record; } else return null; } catch (Exception) { stream.Position = 0; string s = new StreamReader(stream).ReadToEnd(); return null; } } } } }
public void ExportClicked(object sender, EventArgs e) { WriteLog("Starting export"); List<Role> roles = new List<Role>(); URM.UserRoleManager urmServer = new URM.UserRoleManager(); using (urmServer.CreateConnection()) { urmServer.Connection.Open(ConnectionString); WriteLog("Connected to K2 server"); string[] serverRoles = urmServer.GetRoleNameList(); foreach (string role in serverRoles) { WriteLog("Exporting role {0}", role); URM.Role urmRole = urmServer.GetRole(role); Role r = this.CopyToLocalRole(urmRole); roles.Add(r); } WriteLog("Writing {0} roles to XML file", roles.Count); XmlSerializer xmlSer = new XmlSerializer(roles.GetType()); XmlTextWriter writer = new XmlTextWriter(txtFileLocation.Text, Encoding.Unicode); writer.Formatting = Formatting.Indented; xmlSer.Serialize(writer, roles); writer.Flush(); writer.Close(); WriteLog("Closing K2 connection."); } }
/// <summary> /// Enqueue a Web Service record into the record queue /// </summary> public static void EnqueueRequestRecord(RequestRecord newRecord) { bool enableQueueOptimization = false; // turn off the queue optimization (doesn't work with introduction of tags) List<RequestRecord> requests = new List<RequestRecord>(); DataContractJsonSerializer dc = new DataContractJsonSerializer(requests.GetType()); if (newRecord.SerializedBody == null) newRecord.SerializeBody(); using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) { lock (fileLock) { // if the file opens, read the contents using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"RequestRecords.xml", FileMode.OpenOrCreate, file)) { try { // if the file opened, read the record queue requests = dc.ReadObject(stream) as List<RequestRecord>; if (requests == null) requests = new List<RequestRecord>(); } catch (Exception) { stream.Position = 0; string s = new StreamReader(stream).ReadToEnd(); } if (enableQueueOptimization == true) { OptimizeQueue(newRecord, requests); } else { // this is a new record so add the new record at the end requests.Add(newRecord); } // reset the stream and write the new record queue back to the file stream.SetLength(0); stream.Position = 0; dc.WriteObject(stream, requests); stream.Flush(); } } } }
/// <summary> /// Get the first RequestRecord in the queue /// </summary> public static RequestRecord GetRequestRecord() { List<RequestRecord> requests = new List<RequestRecord>(); DataContractJsonSerializer dc = new DataContractJsonSerializer(requests.GetType()); using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) { lock (fileLock) { // try block because the using block below will throw if the file doesn't exist try { // if the file opens, read the contents using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"RequestRecords.xml", FileMode.Open, file)) { try { // if the file opens, read the contents requests = dc.ReadObject(stream) as List<RequestRecord>; if (requests.Count > 0) { RequestRecord record = requests[0]; record.DeserializeBody(); return record; } else return null; } catch (Exception) { stream.Position = 0; string s = new StreamReader(stream).ReadToEnd(); return null; } } } catch (Exception) { // could not open the isolated storage file return null; } } } }
private static string ConstructJson(List<FreeSlot> FreeList) { MemoryStream ms = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(FreeList.GetType()); ser.WriteObject(ms, FreeList); byte[] json = ms.ToArray(); ms.Close(); return Encoding.UTF8.GetString(json, 0, json.Length); }
/// <summary> /// Get all RequestRecords in the queue /// </summary> public static List<RequestRecord> GetAllRequestRecords() { List<RequestRecord> requests = new List<RequestRecord>(); DataContractJsonSerializer dc = new DataContractJsonSerializer(requests.GetType()); using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) { lock (fileLock) { // try block because the using block below will throw if the file doesn't exist try { // if the file opens, read the contents using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"RequestRecords.xml", FileMode.Open, file)) { try { // if the file opens, read the contents requests = dc.ReadObject(stream) as List<RequestRecord>; foreach (var req in requests) { req.DeserializeBody(); } return requests; } catch (Exception) { return null; } } } catch (Exception) { // could not open the isolated storage file return null; } } } }
private void newsRecuperees(object sender, OpenReadCompletedEventArgs e) { if (e.Error == null) { List<News> deserializedUser = new List<News>(); DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType()); deserializedUser = ser.ReadObject(e.Result) as List<News>; foreach (News i in deserializedUser) { News.Add(new NewsViewModel() { prenom = i.Prenom, date = i.date, contenu = i.news, titre = i.titre }); } } }
private void horairesDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { if (e.Error == null) { List<Horaire> deserializedUser = new List<Horaire>(); DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType()); deserializedUser = ser.ReadObject(e.Result) as List<Horaire>; foreach (Horaire i in deserializedUser) { Horaires.Add(new HorairesViewModel() { Jour = i.jour, Heures = i.heures}); } } }
private void annalesRecuperees(object sender, OpenReadCompletedEventArgs e) { if (e.Error == null) { List<Annale> deserializedUser = new List<Annale>(); DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType()); deserializedUser = ser.ReadObject(e.Result) as List<Annale>; foreach (Annale i in deserializedUser) { UVs.Add(new UVViewModel() { NomUV = i.Nom, NbPages = i.Pages }); } } }
private static List<FavoriteRouteAndStop> ReadFavoritesFromDisk(string fileName) { List<FavoriteRouteAndStop> favoritesFromFile = new List<FavoriteRouteAndStop>(); try { using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (appStorage.FileExists(fileName) == true) { using (IsolatedStorageFileStream favoritesFile = appStorage.OpenFile(fileName, FileMode.Open)) { List<Type> knownTypes = new List<Type>(2); knownTypes.Add(typeof(FavoriteRouteAndStop)); knownTypes.Add(typeof(RecentRouteAndStop)); DataContractSerializer serializer = new DataContractSerializer(favoritesFromFile.GetType(), knownTypes); favoritesFromFile = serializer.ReadObject(favoritesFile) as List<FavoriteRouteAndStop>; } // This is required because we changed the data format between versions if (favoritesFromFile.Count > 0 && favoritesFromFile[0].version != FavoriteRouteAndStop.CurrentVersion) { // Currently we don't support backwards compatability, just delete all their favorites/recents appStorage.DeleteFile(fileName); favoritesFromFile = new List<FavoriteRouteAndStop>(); } } else { favoritesFromFile = new List<FavoriteRouteAndStop>(); } } } catch (Exception) { Debug.Assert(false); // We hit an error deserializing the file so delete it if it exists using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (appStorage.FileExists(fileName) == true) { appStorage.DeleteFile(fileName); } } throw; } return favoritesFromFile; }
private static void WriteFavoritesToDisk(List<FavoriteRouteAndStop> favoritesToWrite, string fileName) { using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream favoritesFile = appStorage.OpenFile(fileName, FileMode.Create)) { List<Type> knownTypes = new List<Type>(2); knownTypes.Add(typeof(FavoriteRouteAndStop)); knownTypes.Add(typeof(RecentRouteAndStop)); DataContractSerializer serializer = new DataContractSerializer(favoritesToWrite.GetType(), knownTypes); serializer.WriteObject(favoritesFile, favoritesToWrite); } } }