public async Task <T> LoadLocalData(string filePath) { if (string.IsNullOrEmpty(filePath)) { return(null); } T result = null; //load cache try { var json = await IsolatedStorageHelper.ReadFileAsync(filePath); if (!string.IsNullOrEmpty(json)) { result = JsonSerializer.Deserialize <T>(json); } } catch (Exception ex) { } return(result); }
public async Task <T> LoadLocalData(string module, string file) { if ((string.IsNullOrEmpty(module) || string.IsNullOrEmpty(file))) { return(null); } T result = null; //load cache try { //ensure file existence //await IsolatedStorageHelper.GetFileAsync(moduleName, fileName, Windows.Storage.CreationCollisionOption.OpenIfExists); var json = await IsolatedStorageHelper.ReadFileAsync(module, file); if (!string.IsNullOrEmpty(json)) { result = JsonSerializer.Deserialize <T>(json); } } catch (Exception ex) { } return(result); }
public async void Load(string dataURL, bool cacheData, string module, string file, Action <T> callback) { if (cacheData && (string.IsNullOrEmpty(module) || string.IsNullOrEmpty(file))) { return; } //for callback onCallback = callback; toCacheData = cacheData; moduleName = module; fileName = file; if (!NetworkHelper.CheckInternet()) { //load cache if (cacheData) { try { var cachedJson = await IsolatedStorageHelper.ReadFileAsync(moduleName, fileName); T obj = JsonSerializer.Deserialize <T>(cachedJson); if (obj != null) { App.CurrentInstance.RunAsync(() => { onCallback(obj); }); } } catch (Exception ex) { } } return; } //download new try { HttpWebRequest request = HttpWebRequest.CreateHttp(new Uri(dataURL)); request.Method = "GET"; request.BeginGetResponse(GetData_Callback, request); Loaded = false; Busy = true; } catch (WebException e) { } catch (Exception e) { } }
public static async Task <T> Deserialize <T>(string file) { T val; string content = await IsolatedStorageHelper.ReadFileAsync(file); using (StringReader sr = new StringReader(content)) { XmlSerializer xs = new XmlSerializer(typeof(T)); val = (T)xs.Deserialize(sr); } return(val); }
public static async Task FastIterate(string file, Action <string, string> actionOnEachNode) { string content = await IsolatedStorageHelper.ReadFileAsync(file); if (content.Contains("?>")) { int from = content.IndexOf("?>"); content = content.Substring(from + 2); } string nodeName = string.Empty; string nodeValue = string.Empty; int maxIndex = content.Length - 1; int i = 0; XmlParsingStatus status = XmlParsingStatus.NodeEnded; while (i < maxIndex) { switch (status) { case XmlParsingStatus.BeginningOrEnd: if (content[i] == '/') { status = XmlParsingStatus.EndOfNodeDetected; } else { status = XmlParsingStatus.BeginningOfNodeDetected; } break; case XmlParsingStatus.BeginningOfNodeDetected: if (content[i] == '>') { status = XmlParsingStatus.ValueCollecting; nodeValue = string.Empty; } break; case XmlParsingStatus.ValueCollecting: if (content[i] == '<') { status = XmlParsingStatus.BeginningOrEnd; } else { nodeValue += content[i]; } break; case XmlParsingStatus.EndOfNodeDetected: if (content[i] == '>') { status = XmlParsingStatus.NodeEnded; actionOnEachNode(nodeName, nodeValue); nodeName = string.Empty; nodeValue = string.Empty; } else { nodeName += content[i]; } break; case XmlParsingStatus.NodeEnded: if (content[i] == '<') { status = XmlParsingStatus.BeginningOrEnd; } break; default: break; } i++; } }