Repository navigation
Expand file tree
/
Copy pathXmlService.cs
More file actions
113 lines (96 loc) · 3.49 KB
/
Copy pathXmlService.cs
File metadata and controls
113 lines (96 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xml2SqlImport.Interfaces;
using System.Data;
using Microsoft.Extensions.Configuration;
using System.Xml;
using Xml2SqlImport.Data.SPModels;
using Xml2SqlImport.Helpers.Responses;
namespace Xml2SqlImport.Services
{
public class XmlService : IXmlService
{
private readonly ILocalDataService _dbService;
private readonly IConfiguration _config;
private readonly IAppSettings _settings;
public XmlService(ILocalDataService dbService, IConfiguration config, IAppSettings settings)
{
_dbService = dbService;
_config = config;
_settings = settings;
}
public RetVal<DataSet> readXml(LocalXmlInfo xmlInfo)
{
var xmlDs = new DataSet("xmlFile");
var ret = new RetVal<DataSet> { success = false, errorMessage = "Could not read the XML file" };
try
{
var rd = XmlReader.Create(xmlInfo.localXmlFile!);
xmlDs.ReadXmlSchema(xmlInfo.localXsdFile!);
var clean = cleanDs(xmlDs);
if (clean.success)
{
xmlDs = clean.returnVal;
xmlDs!.ReadXml(rd);
}
rd.Dispose();
ret.success = true;
ret.errorMessage = null;
ret.returnVal = xmlDs;
// fill the tableList table
var tblList = new utp_IMPORT_TableList();
xmlDs!.Tables.OfType<DataTable>().Select(t => t.TableName).ToList().ForEach(t => tblList.Rows.Add($"{_settings.currentFile!.tablePrefix}{t}"));
xmlInfo.tableList = tblList;
}
catch (Exception e)
{
ret.errorMessage = e.Message;
Console.WriteLine(e.Message);
}
return ret;
}
// this function checks for duplicate fieldnames in the dataset
// and removes them
private RetVal<DataSet> cleanDs(DataSet ds)
{
var ret = new RetVal<DataSet> { success = false, errorMessage = "Could not read the XML file" };
foreach (DataTable tbl in ds.Tables)
{
var lst = new Dictionary<string, int>();
var dbl = new Dictionary<string, int>();
// check if a column exist ing the table
// if so, add it to the doubles dictionary
foreach (DataColumn col in tbl.Columns)
{
var nm = col.ColumnName;
var idx = tbl.Columns.IndexOf(nm);
var found = lst.ContainsKey(nm.ToLower());
if (found)
{
dbl.Add(nm.ToLower(), idx);
}
else
{
lst.Add(nm.ToLower(), idx);
}
}
// remove all the doubles from the list dictionary
if (dbl.Count > 0)
{
foreach (int idx in dbl.Values)
{
tbl.Columns.RemoveAt(idx);
}
}
}
ret.returnVal = ds;
ret.success = true;
ret.errorMessage = null;
return ret;
}
}
}