public bool ReadXml(string xmlName, out string errorMessage) { errorMessage = null; Clear(); if (string.IsNullOrEmpty(xmlName)) { return(false); } if (!File.Exists(xmlName)) { return(false); } string xmlDir = Path.GetDirectoryName(xmlName); try { string prefix = string.Empty; XmlDocument xdocConfig = XmlDocumentLoader.LoadWithIncludes(xmlName); XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xdocConfig.NameTable); XPathNavigator xNav = xdocConfig.CreateNavigator(); if (xNav.MoveToFollowing(XPathNodeType.Element)) { IDictionary <string, string> localNamespaces = xNav.GetNamespacesInScope(XmlNamespaceScope.Local); if (localNamespaces.TryGetValue("", out string nameSpace)) { namespaceManager.AddNamespace("carcontrol", nameSpace); prefix = "carcontrol:"; } } XmlAttribute attrib; XmlNode xnodeGlobal = xdocConfig.SelectSingleNode(string.Format("/{0}configuration/{0}global", prefix), namespaceManager); if (xnodeGlobal?.Attributes != null) { attrib = xnodeGlobal.Attributes["ecu_path"]; if (attrib != null) { if (Path.IsPathRooted(attrib.Value)) { _ecuPath = attrib.Value; } else { // ReSharper disable once AssignNullToNotNullAttribute _ecuPath = string.IsNullOrEmpty(xmlDir) ? attrib.Value : Path.Combine(xmlDir, attrib.Value); } } attrib = xnodeGlobal.Attributes["log_path"]; if (attrib != null) { _logPath = attrib.Value; } attrib = xnodeGlobal.Attributes["append_log"]; if (attrib != null) { _appendLog = XmlConvert.ToBoolean(attrib.Value); } attrib = xnodeGlobal.Attributes["manufacturer"]; if (attrib != null) { _manufacturerName = attrib.Value; } attrib = xnodeGlobal.Attributes["interface"]; if (attrib != null) { _interfaceName = attrib.Value; } } if (string.Compare(_manufacturerName, "Audi", StringComparison.OrdinalIgnoreCase) == 0) { _manufacturerType = ActivityCommon.ManufacturerType.Audi; } else if (string.Compare(_manufacturerName, "Seat", StringComparison.OrdinalIgnoreCase) == 0) { _manufacturerType = ActivityCommon.ManufacturerType.Seat; } else if (string.Compare(_manufacturerName, "Skoda", StringComparison.OrdinalIgnoreCase) == 0) { _manufacturerType = ActivityCommon.ManufacturerType.Skoda; } else if (string.Compare(_manufacturerName, "VW", StringComparison.OrdinalIgnoreCase) == 0) { _manufacturerType = ActivityCommon.ManufacturerType.Vw; } else { _manufacturerType = ActivityCommon.ManufacturerType.Bmw; } bool isBmw = _manufacturerType == ActivityCommon.ManufacturerType.Bmw; if (isBmw && string.Compare(_interfaceName, "ENET", StringComparison.OrdinalIgnoreCase) == 0) { _interfaceType = ActivityCommon.InterfaceType.Enet; } else if (isBmw && string.Compare(_interfaceName, "ELMWIFI", StringComparison.OrdinalIgnoreCase) == 0) { _interfaceType = ActivityCommon.InterfaceType.ElmWifi; } else if (string.Compare(_interfaceName, "DEEPOBDWIFI", StringComparison.OrdinalIgnoreCase) == 0) { _interfaceType = ActivityCommon.InterfaceType.DeepObdWifi; } else if (isBmw && string.Compare(_interfaceName, "FTDI", StringComparison.OrdinalIgnoreCase) == 0) { _interfaceType = ActivityCommon.InterfaceType.Ftdi; } else { _interfaceType = ActivityCommon.InterfaceType.Bluetooth; } XmlNode xnodePages = xdocConfig.SelectSingleNode(string.Format("/{0}configuration/{0}pages", prefix), namespaceManager); if (xnodePages?.Attributes != null) { attrib = xnodePages.Attributes["include_filename"]; if (attrib != null) { _xmlFileNamePages = attrib.Value; } } XmlNodeList xnodesPage = xdocConfig.SelectNodes(string.Format("/{0}configuration/{0}pages/{0}page", prefix), namespaceManager); if (xnodesPage != null) { foreach (XmlNode xnodePage in xnodesPage) { string pageName = string.Empty; string xmlFileName = string.Empty; float pageWeight = -1; int textResId = 0; int gaugesPortrait = GaugesPortraitDefault; int gaugesLandscape = GaugesLandscapeDefault; PageInfo.DisplayModeType displayMode = PageInfo.DisplayModeType.List; string logFile = string.Empty; bool jobActivate = false; if (xnodePage.Attributes != null) { attrib = xnodePage.Attributes["include_filename"]; if (attrib != null) { xmlFileName = attrib.Value; } attrib = xnodePage.Attributes["name"]; if (attrib != null) { pageName = attrib.Value; } attrib = xnodePage.Attributes["weight"]; if (attrib != null) { try { pageWeight = XmlConvert.ToSingle(attrib.Value); } catch { // ignored } } attrib = xnodePage.Attributes["display-mode"]; if (attrib != null) { if (!Enum.TryParse(attrib.Value, true, out displayMode)) { displayMode = PageInfo.DisplayModeType.List; } } attrib = xnodePage.Attributes["fontsize"]; if (attrib != null) { string size = attrib.Value.ToLowerInvariant(); switch (size) { case "small": textResId = Android.Resource.Style.TextAppearanceSmall; break; case "medium": textResId = Android.Resource.Style.TextAppearanceMedium; break; case "large": textResId = Android.Resource.Style.TextAppearanceLarge; break; } } attrib = xnodePage.Attributes["gauges-portrait"]; if (attrib != null) { try { int gauges = XmlConvert.ToInt32(attrib.Value); if (gauges >= 1) { gaugesPortrait = gauges; } } catch { // ignored } } attrib = xnodePage.Attributes["gauges-landscape"]; if (attrib != null) { try { int gauges = XmlConvert.ToInt32(attrib.Value); if (gauges >= 1) { gaugesLandscape = gauges; } } catch { // ignored } } attrib = xnodePage.Attributes["logfile"]; if (attrib != null) { logFile = attrib.Value; } attrib = xnodePage.Attributes["activate"]; if (attrib != null) { try { jobActivate = XmlConvert.ToBoolean(attrib.Value); } catch (Exception) { // ignored } } } JobsInfo jobsInfo = null; ErrorsInfo errorsInfo = null; List <DisplayInfo> displayList = new List <DisplayInfo>(); List <StringInfo> stringList = new List <StringInfo>(); bool logEnabled = false; string classCode = null; bool codeShowWarnings = false; foreach (XmlNode xnodePageChild in xnodePage.ChildNodes) { ReadDisplayNode(xnodePageChild, displayList, null, ref logEnabled); if (string.Compare(xnodePageChild.Name, "strings", StringComparison.OrdinalIgnoreCase) == 0) { string lang = null; if (xnodePageChild.Attributes != null) { attrib = xnodePageChild.Attributes["lang"]; if (attrib != null) { lang = attrib.Value; } } Dictionary <string, string> stringDict = new Dictionary <string, string>(); foreach (XmlNode xnodeString in xnodePageChild.ChildNodes) { string text = xnodeString.InnerText; string name = string.Empty; if (xnodeString.Attributes != null) { attrib = xnodeString.Attributes["name"]; if (attrib != null) { name = attrib.Value; } } if (string.IsNullOrEmpty(name)) { continue; } if (!stringDict.ContainsKey(name)) { stringDict.Add(name, text); } } stringList.Add(new StringInfo(lang, stringDict)); } if (string.Compare(xnodePageChild.Name, "jobs", StringComparison.OrdinalIgnoreCase) == 0) { string sgbd = null; string vagDataFileName = null; string vagUdsFileName = null; List <JobInfo> jobList = new List <JobInfo>(); if (xnodePageChild.Attributes != null) { attrib = xnodePageChild.Attributes["sgbd"]; if (attrib != null) { sgbd = attrib.Value; } attrib = xnodePageChild.Attributes["vag_data_file"]; if (attrib != null) { vagDataFileName = attrib.Value; } attrib = xnodePageChild.Attributes["vag_uds_file"]; if (attrib != null) { vagUdsFileName = attrib.Value; } } foreach (XmlNode xnodeJobsChild in xnodePageChild.ChildNodes) { if (string.Compare(xnodeJobsChild.Name, "job", StringComparison.OrdinalIgnoreCase) == 0) { string jobId = string.Empty; string jobSgbd = string.Empty; string jobName = string.Empty; string jobArgsFirst = string.Empty; string jobArgs = string.Empty; string jobResults = string.Empty; if (xnodeJobsChild.Attributes != null) { attrib = xnodeJobsChild.Attributes["id"]; if (attrib != null) { jobId = attrib.Value; } attrib = xnodeJobsChild.Attributes["sgbd"]; if (attrib != null) { jobSgbd = attrib.Value; } attrib = xnodeJobsChild.Attributes["name"]; if (attrib != null) { jobName = attrib.Value; } attrib = xnodeJobsChild.Attributes["args_first"]; if (attrib != null) { jobArgsFirst = attrib.Value; } attrib = xnodeJobsChild.Attributes["args"]; if (attrib != null) { jobArgs = attrib.Value; } attrib = xnodeJobsChild.Attributes["results"]; if (attrib != null) { jobResults = attrib.Value; } } jobList.Add(new JobInfo(jobId, jobSgbd, jobName, jobArgsFirst, jobArgs, jobResults)); foreach (XmlNode xnodeJobChild in xnodeJobsChild.ChildNodes) { ReadDisplayNode(xnodeJobChild, displayList, (string.IsNullOrEmpty(jobId) ? jobName : jobId) + "#", ref logEnabled); } } } jobsInfo = new JobsInfo(sgbd, vagDataFileName, vagUdsFileName, jobList); } if (string.Compare(xnodePageChild.Name, "read_errors", StringComparison.OrdinalIgnoreCase) == 0) { string sgbdFunctional = string.Empty; attrib = xnodePageChild.Attributes["sgbd_functional"]; if (attrib != null) { sgbdFunctional = attrib.Value; } List <EcuInfo> ecuList = new List <EcuInfo>(); foreach (XmlNode xnodeErrorsChild in xnodePageChild.ChildNodes) { if (string.Compare(xnodeErrorsChild.Name, "ecu", StringComparison.OrdinalIgnoreCase) == 0) { string ecuName = string.Empty; string sgbd = string.Empty; string vagDataFileName = null; string vagUdsFileName = null; string results = "F_UW_KM"; if (xnodeErrorsChild.Attributes != null) { attrib = xnodeErrorsChild.Attributes["name"]; if (attrib != null) { ecuName = attrib.Value; } attrib = xnodeErrorsChild.Attributes["sgbd"]; if (attrib != null) { sgbd = attrib.Value; } attrib = xnodeErrorsChild.Attributes["vag_data_file"]; if (attrib != null) { vagDataFileName = attrib.Value; } attrib = xnodeErrorsChild.Attributes["vag_uds_file"]; if (attrib != null) { vagUdsFileName = attrib.Value; } attrib = xnodeErrorsChild.Attributes["results"]; if (attrib != null) { results = attrib.Value; } } ecuList.Add(new EcuInfo(ecuName, sgbd, vagDataFileName, vagUdsFileName, results)); } } errorsInfo = new ErrorsInfo(sgbdFunctional, ecuList); } if (string.Compare(xnodePageChild.Name, "code", StringComparison.OrdinalIgnoreCase) == 0) { classCode = xnodePageChild.InnerText; attrib = xnodePageChild.Attributes["show_warnings"]; // ReSharper disable once ConvertIfStatementToNullCoalescingExpression if (attrib == null) { // for backward compatibility attrib = xnodePageChild.Attributes["show_warnigs"]; } if (attrib != null) { try { codeShowWarnings = XmlConvert.ToBoolean(attrib.Value); } catch (Exception) { // ignored } } } } if (!logEnabled) { logFile = string.Empty; } if (logEnabled) { _logTagsPresent = true; } if (string.IsNullOrEmpty(pageName)) { continue; } if (string.IsNullOrWhiteSpace(classCode)) { classCode = null; } _pageList.Add(new PageInfo(xmlFileName, pageName, pageWeight, displayMode, textResId, gaugesPortrait, gaugesLandscape, logFile, jobActivate, classCode, codeShowWarnings, jobsInfo, errorsInfo, displayList, stringList)); } } return(true); } catch (Exception ex) { errorMessage = string.Empty; string fileName = Path.GetFileName(xmlName); if (!string.IsNullOrEmpty(fileName)) { errorMessage = fileName + ":\r\n"; } errorMessage += EdiabasNet.GetExceptionText(ex) ?? string.Empty; return(false); } }
public bool ReadXml(string xmlName) { _pageList.Clear(); if (string.IsNullOrEmpty(xmlName)) { return(false); } if (!File.Exists(xmlName)) { return(false); } string xmlDir = Path.GetDirectoryName(xmlName); _ecuPath = string.Empty; _logPath = string.Empty; _interfaceName = string.Empty; try { string prefix = string.Empty; XmlDocument xdocConfig = XmlDocumentLoader.LoadWithIncludes(xmlName); XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xdocConfig.NameTable); XPathNavigator xNav = xdocConfig.CreateNavigator(); if (xNav.MoveToFollowing(XPathNodeType.Element)) { IDictionary <string, string> localNamespaces = xNav.GetNamespacesInScope(XmlNamespaceScope.Local); string nameSpace; if (localNamespaces.TryGetValue("", out nameSpace)) { namespaceManager.AddNamespace("carcontrol", nameSpace); prefix = "carcontrol:"; } } XmlAttribute attrib; XmlNode xnodeGlobal = xdocConfig.SelectSingleNode(string.Format("/{0}configuration/{0}global", prefix), namespaceManager); if (xnodeGlobal?.Attributes != null) { attrib = xnodeGlobal.Attributes["ecu_path"]; if (attrib != null) { if (Path.IsPathRooted(attrib.Value)) { _ecuPath = attrib.Value; } else { _ecuPath = string.IsNullOrEmpty(xmlDir) ? attrib.Value : Path.Combine(xmlDir, attrib.Value); } } attrib = xnodeGlobal.Attributes["log_path"]; if (attrib != null) { _logPath = attrib.Value; } attrib = xnodeGlobal.Attributes["append_log"]; if (attrib != null) { _appendLog = XmlConvert.ToBoolean(attrib.Value); } attrib = xnodeGlobal.Attributes["interface"]; if (attrib != null) { _interfaceName = attrib.Value; } } if (string.Compare(_interfaceName, "ENET", StringComparison.OrdinalIgnoreCase) == 0) { _interfaceType = ActivityCommon.InterfaceType.Enet; } else if (string.Compare(_interfaceName, "FTDI", StringComparison.OrdinalIgnoreCase) == 0) { _interfaceType = ActivityCommon.InterfaceType.Ftdi; } else { _interfaceType = ActivityCommon.InterfaceType.Bluetooth; } XmlNodeList xnodePages = xdocConfig.SelectNodes(string.Format("/{0}configuration/{0}pages/{0}page", prefix), namespaceManager); if (xnodePages != null) { foreach (XmlNode xnodePage in xnodePages) { string pageName = string.Empty; float pageWeight = -1; string logFile = string.Empty; bool jobActivate = false; if (xnodePage.Attributes != null) { attrib = xnodePage.Attributes["name"]; if (attrib != null) { pageName = attrib.Value; } attrib = xnodePage.Attributes["weight"]; if (attrib != null) { try { pageWeight = XmlConvert.ToSingle(attrib.Value); } catch { // ignored } } attrib = xnodePage.Attributes["logfile"]; if (attrib != null) { logFile = attrib.Value; } attrib = xnodePage.Attributes["activate"]; if (attrib != null) { jobActivate = XmlConvert.ToBoolean(attrib.Value); } } JobsInfo jobsInfo = null; ErrorsInfo errorsInfo = null; List <DisplayInfo> displayList = new List <DisplayInfo>(); List <StringInfo> stringList = new List <StringInfo>(); bool logEnabled = false; string classCode = null; bool codeShowWarnings = false; foreach (XmlNode xnodePageChild in xnodePage.ChildNodes) { ReadDisplayNode(xnodePageChild, displayList, null, ref logEnabled); if (string.Compare(xnodePageChild.Name, "strings", StringComparison.OrdinalIgnoreCase) == 0) { string lang = null; if (xnodePageChild.Attributes != null) { attrib = xnodePageChild.Attributes["lang"]; if (attrib != null) { lang = attrib.Value; } } Dictionary <string, string> stringDict = new Dictionary <string, string>(); foreach (XmlNode xnodeString in xnodePageChild.ChildNodes) { string text = xnodeString.InnerText; string name = string.Empty; if (xnodeString.Attributes != null) { attrib = xnodeString.Attributes["name"]; if (attrib != null) { name = attrib.Value; } } if (string.IsNullOrEmpty(name)) { continue; } if (!stringDict.ContainsKey(name)) { stringDict.Add(name, text); } } stringList.Add(new StringInfo(lang, stringDict)); } if (string.Compare(xnodePageChild.Name, "jobs", StringComparison.OrdinalIgnoreCase) == 0) { string sgbd = null; List <JobInfo> jobList = new List <JobInfo>(); if (xnodePageChild.Attributes != null) { attrib = xnodePageChild.Attributes["sgbd"]; if (attrib != null) { sgbd = attrib.Value; } } foreach (XmlNode xnodeJobsChild in xnodePageChild.ChildNodes) { if (string.Compare(xnodeJobsChild.Name, "job", StringComparison.OrdinalIgnoreCase) == 0) { string jobName = string.Empty; string jobArgsFirst = string.Empty; string jobArgs = string.Empty; string jobResults = string.Empty; if (xnodeJobsChild.Attributes != null) { attrib = xnodeJobsChild.Attributes["name"]; if (attrib != null) { jobName = attrib.Value; } attrib = xnodeJobsChild.Attributes["args_first"]; if (attrib != null) { jobArgsFirst = attrib.Value; } attrib = xnodeJobsChild.Attributes["args"]; if (attrib != null) { jobArgs = attrib.Value; } attrib = xnodeJobsChild.Attributes["results"]; if (attrib != null) { jobResults = attrib.Value; } } jobList.Add(new JobInfo(jobName, jobArgsFirst, jobArgs, jobResults)); foreach (XmlNode xnodeJobChild in xnodeJobsChild.ChildNodes) { ReadDisplayNode(xnodeJobChild, displayList, jobName + "#", ref logEnabled); } } } jobsInfo = new JobsInfo(sgbd, jobList); } if (string.Compare(xnodePageChild.Name, "read_errors", StringComparison.OrdinalIgnoreCase) == 0) { List <EcuInfo> ecuList = new List <EcuInfo>(); foreach (XmlNode xnodeErrorsChild in xnodePageChild.ChildNodes) { if (string.Compare(xnodeErrorsChild.Name, "ecu", StringComparison.OrdinalIgnoreCase) == 0) { string ecuName = string.Empty; string sgbd = string.Empty; string results = "F_UW_KM"; if (xnodeErrorsChild.Attributes != null) { attrib = xnodeErrorsChild.Attributes["name"]; if (attrib != null) { ecuName = attrib.Value; } attrib = xnodeErrorsChild.Attributes["sgbd"]; if (attrib != null) { sgbd = attrib.Value; } attrib = xnodeErrorsChild.Attributes["results"]; if (attrib != null) { results = attrib.Value; } } ecuList.Add(new EcuInfo(ecuName, sgbd, results)); } } errorsInfo = new ErrorsInfo(ecuList); } if (string.Compare(xnodePageChild.Name, "code", StringComparison.OrdinalIgnoreCase) == 0) { classCode = xnodePageChild.InnerText; attrib = xnodePageChild.Attributes["show_warnigs"]; if (attrib != null) { codeShowWarnings = XmlConvert.ToBoolean(attrib.Value); } } } if (!logEnabled) { logFile = string.Empty; } if (string.IsNullOrEmpty(pageName)) { continue; } if (string.IsNullOrWhiteSpace(classCode)) { classCode = null; } _pageList.Add(new PageInfo(pageName, pageWeight, logFile, jobActivate, classCode, codeShowWarnings, jobsInfo, errorsInfo, displayList, stringList)); } } return(true); } catch (Exception) { return(false); } }