private void OnGameListDownload(object sender, DownloadDataCompletedEventArgs e) { if (e.Error == null && !e.Cancelled) { MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(e.Result, 0, e.Result.Length); memoryStream.Seek(0L, SeekOrigin.Begin); XPathNodeIterator xpathNodeIterator = new XPathDocument((Stream)memoryStream).CreateNavigator().Select("/games/game"); while (xpathNodeIterator.MoveNext()) { string type = xpathNodeIterator.Current.GetAttribute("type", ""); if (type == string.Empty) { type = "normal"; } this.AddGame(xpathNodeIterator.Current.ValueAsLong, type); } } else { this.AddDefaultGames(); } this.RefreshGames(); this.refreshGamesButton.Enabled = true; this.DownloadNextLogo(); }
private static string GetInstallation(string jobFile) { string fileName = ""; try { using (XmlTextReader xmlTextReader = new XmlTextReader(jobFile)) { XPathNodeIterator xpathNodeIterator = new XPathDocument(xmlTextReader, XmlSpace.Preserve).CreateNavigator().Select(XMLNodes.JOB_XML_PATH_NODE_INSTALLATIONS); while (xpathNodeIterator.MoveNext()) { fileName = xpathNodeIterator.Current.GetAttribute("filename", ""); } } } catch (FileNotFoundException) { throw new FileNotFoundException("File not found"); } catch (Exception) { throw; } return(fileName); }
private void GetLabelsForJob(string jobFile) { try { using (XmlTextReader xmlTextReader = new XmlTextReader(jobFile)) { XPathNodeIterator xpathNodeIterator = new XPathDocument(xmlTextReader, XmlSpace.Preserve).CreateNavigator().Select(XMLNodes.JOB_XML_PATH_Label); while (xpathNodeIterator.MoveNext()) { string labelFileName = xpathNodeIterator.Current.GetAttribute("filename", ""); string groupName = xpathNodeIterator.Current.GetAttribute("group", ""); try { string pathLabelFileName = Path.GetPathRoot(jobFile) + NameFolders.LABELS + labelFileName; GetObjectsFromLabel(groupName, pathLabelFileName); } catch (Exception) { throw; } } } } catch (FileNotFoundException) { throw new FileNotFoundException("File not found"); } catch (Exception) { throw; } }
public List <DTOLabelSettings> Load(string filePath) { try { List <DTOLabelSettings> _listDTO = new List <DTOLabelSettings>(); using (XmlTextReader xmlTextReader = new XmlTextReader(filePath)) { XPathNodeIterator xpathNodeIterator = new XPathDocument(xmlTextReader, XmlSpace.Preserve).CreateNavigator().Select(XMLNodes.LABEL_SETTINGS); while (xpathNodeIterator.MoveNext()) { int outputControl; int dataField; string groupName = xpathNodeIterator.Current.GetAttribute("groupName", ""); //string labelName = xpathNodeIterator.Current.GetAttribute("labelName", ""); string objectName = xpathNodeIterator.Current.GetAttribute("objectName", ""); string contentName = xpathNodeIterator.Current.GetAttribute("contentName", ""); int.TryParse(xpathNodeIterator.Current.GetAttribute("outputControl", ""), out outputControl); int.TryParse(xpathNodeIterator.Current.GetAttribute("dataField", ""), out dataField); DTOLabelSettings dto = new DTOLabelSettings(groupName, "", objectName, contentName, outputControl, dataField); _listDTO.Add(dto); } } return(_listDTO); } catch (FileNotFoundException) { throw new FileNotFoundException("File not found"); } catch (Exception) { throw new Exception("Nieprawidłowa struktura pliku z ustawieniami etykiety."); } }
/// <summary> /// Adds XML to the specified data path. /// </summary> /// <param name="sourceXml">XML to add at the path.</param> /// <param name="sourcePath">Source XPath, or null for root.</param> /// <param name="targetPath">Target XPath, or null for root.</param> /// <param name="overwrite">Set true to delete data first.</param> public void Add(IXPathNavigable sourceXml, string?sourcePath, string?targetPath, bool overwrite) { // Validate if (sourceXml == null) { throw new ArgumentNullException(nameof(sourceXml)); } // Use root elements as source when null or root if (string.IsNullOrEmpty(sourcePath) || sourcePath == "/") { sourcePath = "/*"; } // Read source XML and make anonymous var buffer = new StringBuilder(); var readSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment }; var writeSettings = new XmlWriterSettings { ConformanceLevel = ConformanceLevel.Fragment }; using (var reader = XmlReader.Create(sourceXml.CreateNavigator().ReadSubtree(), readSettings)) using (var writer = XmlWriter.Create(buffer, writeSettings)) XmlFullExtensions.FormatXmlAnyTransform.Transform(reader, writer); // Find or create target path var target = GetPath(targetPath, true); if (target is null) { throw new ArgumentOutOfRangeException(nameof(targetPath)); } // Append or replace source XML depending on overwrite option using (var reader = XmlReader.Create(new StringReader(buffer.ToString()), readSettings)) { var sourceNodes = new XPathDocument(reader).CreateNavigator().Select(sourcePath); while (sourceNodes.MoveNext()) { Debug.Assert(sourceNodes.Current != null); if (overwrite) { target.ReplaceSelf(sourceNodes.Current); } else { target.AppendChild(sourceNodes.Current); } } } }
private static void MaybeMapException(WebException we) { var response = we.Response; if (response == null || response.ContentLength == 0) { return; } try { using var stream = response.GetResponseStream(); if (stream == null) { return; } if (response.ContentType.StartsWith("application/xml")) { Debug.Print($"[{DateTime.UtcNow}] => RESPONSE ({response.ContentType}): <{response.ContentLength} byte(s)>"); StringBuilder?sb = null; var xpath = new XPathDocument(stream).CreateNavigator().Select("/error/text"); while (xpath.MoveNext()) { if (sb == null) { sb = new StringBuilder(); } else { sb.AppendLine(); } sb.Append(xpath.Current?.InnerXml); } if (sb == null) { Debug.Print($"[{DateTime.UtcNow}] => NO ERROR TEXT FOUND"); return; } Debug.Print($"[{DateTime.UtcNow}] => ERROR: \"{sb}\""); throw new QueryException(sb.ToString(), we); } if (response.ContentType.StartsWith("application/json")) { var characterSet = "utf-8"; if (response is HttpWebResponse httpResponse) { characterSet = httpResponse.CharacterSet; if (string.IsNullOrWhiteSpace(characterSet)) { characterSet = "utf-8"; } } var enc = Encoding.GetEncoding(characterSet); using var sr = new StreamReader(stream, enc); var json = sr.ReadToEnd(); Debug.Print($"[{DateTime.UtcNow}] => RESPONSE ({response.ContentType}): \"{JsonUtils.Prettify(json)}\""); var moe = Query.Deserialize <MessageOrError>(json); if (moe?.Error == null) { Debug.Print($"[{DateTime.UtcNow}] => NO ERROR TEXT FOUND"); return; } Debug.Print($"[{DateTime.UtcNow}] => ERROR: \"{moe.Error}\" ({moe.Help})"); throw new QueryException(moe.Error, we) { HelpLink = moe.Help }; } Debug.Print($"[{DateTime.UtcNow}] => UNHANDLED ERROR RESPONSE ({response.ContentType}): <{response.ContentLength} byte(s)>"); } catch (QueryException) { throw; } catch (Exception e) { Debug.Print($"[{DateTime.UtcNow}] => FAILED TO PROCESS ERROR RESPONSE: [{e.GetType()}] {e.Message}"); /* keep calm and fall through */ } }
#pragma warning restore 649 private static string ExtractError(WebResponse response) { if (response == null || response.ContentLength == 0) { return(null); } try { using (var stream = response.GetResponseStream()) { if (stream == null) { return(null); } if (response.ContentType.StartsWith("application/xml")) { Debug.Print($"[{DateTime.UtcNow}] => RESPONSE ({response.ContentType}): <{response.ContentLength} byte(s)>"); StringBuilder sb = null; var xpath = new XPathDocument(stream).CreateNavigator().Select("/error/text"); while (xpath.MoveNext()) { if (sb == null) { sb = new StringBuilder(); } else { sb.AppendLine(); } sb.Append(xpath.Current.InnerXml); } Debug.Print($"[{DateTime.UtcNow}] => ERROR: \"{sb}\""); return(sb?.ToString()); } if (response.ContentType.StartsWith("application/json")) { var encname = "utf-8"; if (response is HttpWebResponse httpresponse) { encname = httpresponse.CharacterSet; if (encname == null || encname.Trim().Length == 0) { encname = "utf-8"; } } var enc = Encoding.GetEncoding(encname); using (var sr = new StreamReader(stream, enc)) { var json = sr.ReadToEnd(); Debug.Print($"[{DateTime.UtcNow}] => RESPONSE ({response.ContentType}): \"{json}\""); var moe = JsonConvert.DeserializeObject <MessageOrError>(json); Debug.Print($"[{DateTime.UtcNow}] => ERROR: \"{moe?.error}\""); return(moe?.error); } } Debug.Print($"[{DateTime.UtcNow}] => UNHANDLED ERROR RESPONSE ({response.ContentType}): <{response.ContentLength} byte(s)>"); } } catch (Exception e) { Debug.Print($"[{DateTime.UtcNow}] => FAILED TO PROCESS ERROR RESPONSE: [{e.GetType()}] {e.Message}"); /* keep calm and fall through */ } return(null); }