public static RoboFormData[] ParseFile(string filePath, List <string> importMessages, out RoboformExportType currentRBExportType) { if (filePath == null) { throw new ArgumentNullException("filePath"); } if (importMessages == null) { throw new ArgumentNullException("importMessages"); } HtmlDocument html = new HtmlDocument(); try { string source = System.IO.File.ReadAllText(filePath, Encoding.ASCII).Replace(@"­", ""); source = System.Net.WebUtility.HtmlDecode(source); html.LoadHtml(source); } catch { throw new FileLoadException(string.Format(CultureInfo.InvariantCulture, "Error while loading file '{0}'.", filePath)); } return(ParseFile(html, importMessages, out currentRBExportType)); }
private static RoboFormData[] ParseFile(HtmlDocument html, List <string> importMessages, out RoboformExportType currentRBExportType) { if (html == null) { throw new ArgumentNullException("html"); } if (importMessages == null) { throw new ArgumentNullException("importMessages"); } currentRBExportType = GetExportType(html); int lncnt = 0; List <RoboFormData> dataList = new List <RoboFormData>(); RoboFormData roboData = null; KVSwitch currKV = KVSwitch.Key; string currKey = ""; string currValue = ""; string currentIdentityName = ""; foreach (var d in GetBody(html)) { lncnt++; try { if (d.IsCaption()) { currKV = KVSwitch.Key; currKey = ""; currValue = ""; if (roboData != null) { roboData.IsAdded = true; dataList.Add(roboData); } roboData = new RoboFormData(); if (d.ClassEqualsTo("caption")) { currentIdentityName = d.InnerText; } if (NeedToReadInnerText(d)) { roboData.Caption = d.InnerText; // <TD> node also can be "caption" class (no text in it, reason unknown) } roboData.IdentityName = currentIdentityName; } else if (d.ClassEqualsTo("subcaption")) { if (!string.IsNullOrEmpty(d.InnerText)) { roboData.SubCaption = d.InnerText; } } else if (d.ClassEqualsTo("field")) { roboData.DataList.Add(d.InnerHtml.Replace("<br>", " ").Replace("<br/>", " ").Replace("<br />", " ")); roboData.IsDictionary = IsDictionary(d); switch (currKV) { case KVSwitch.Key: currKey = d.InnerText.Replace(":", ""); currKV = KVSwitch.Value; break; case KVSwitch.Value: currValue = d.InnerHtml.Replace("<br>", " ").Replace("<br/>", " ").Replace("<br />", " "); while (roboData.Data.ContainsKey(currKey)) { currKey += "_"; } roboData.Data.Add(currKey, currValue); currKV = KVSwitch.Key; break; } } } catch (Exception ex) { if (ex is IndexOutOfRangeException || ex is ArgumentException || ex is NullReferenceException) { importMessages.Add(string.Format(CultureInfo.InvariantCulture, "Error in line:{0}.", lncnt)); } else { throw; } } } if ((roboData != null) && (!roboData.IsAdded)) { dataList.Add(roboData); } return(dataList.ToArray()); }