Repository navigation
Expand file tree
/
Copy pathXsdService.cs
More file actions
122 lines (94 loc) · 4.31 KB
/
Copy pathXsdService.cs
File metadata and controls
122 lines (94 loc) · 4.31 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
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using Xml2SqlImport.Data.XmlModels;
using Xml2SqlImport.Interfaces;
namespace Xml2SqlImport.Services
{
internal class XsdService : IXsdService
{
public bool readXsdSchemas(string fileName)
{
var xsdPath = Path.Combine("C:\\ProgramData\\Internal\\Repos", "Files", fileName);
XmlSchemaSet schemaSet = new XmlSchemaSet();
//schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add("http://www.tempuri.org", xsdPath);
schemaSet.Compile();
// Retrieve the compiled XmlSchema object from the XmlSchemaSet
// by iterating over the Schemas property.
XmlSchema? schema;
foreach (XmlSchema customerSchema in schemaSet.Schemas())
{
schema = customerSchema;
// Load the XSD _file into an XmlSchema object
//XmlSchema? schema = XmlSchema.Read(new XmlTextReader(xsdPath), null);
if (schema != null)
{
// Create a dictionary to store the tables and fields
Dictionary<string, List<XmlTableField>> tables = new Dictionary<string, List<XmlTableField>>();
// Iterate over the schema's global elements
foreach (XmlSchemaElement element in schema.Elements.Values)
{
// Get the element's type
XmlSchemaType? type = element.ElementSchemaType;
// If the type is a complex type
if (type is XmlSchemaComplexType)
{
_readComplexType("",element, type, tables);
}
}
// Iterate over the tables in the dictionary
foreach (KeyValuePair<string, List<XmlTableField>> table in tables)
{
Console.WriteLine("Table: " + table.Key);
Console.WriteLine("Fields: ");
foreach (XmlTableField field in table.Value)
{
Console.WriteLine(" - " + field.fieldName + ": " + field.fieldType);
}
}
}
}
return true;
}
void _readComplexType(string parentName,XmlSchemaElement element, XmlSchemaType type, Dictionary<string, List<XmlTableField>> tables)
{
// Get the complex type
XmlSchemaComplexType complexType = (XmlSchemaComplexType)type;
// Get the complex type's sequence (if it has one)
XmlSchemaSequence? sequence = complexType.Particle as XmlSchemaSequence;
// If the complex type has a sequence
if (sequence != null)
{
// Create a list to store the fields for this table
List<XmlTableField> fields = new List<XmlTableField>();
// Iterate over the sequence's items
foreach (XmlSchemaElement field in sequence.Items)
{
// Get the element's type
XmlSchemaType? fType = field.ElementSchemaType;
// Get the parent element in case of same table name
//XmlSchemaSequence? parent = field.;
//var i = parent.i
if (fType is XmlSchemaComplexType)
{
_readComplexType($"{parentName}_{element.Name!}", field, fType, tables);
} else
{
XmlSchemaType tpe = field.ElementSchemaType!;
// Add the field to the list
var fld = new XmlTableField { fieldName = field.Name!, fieldType = tpe!.TypeCode.ToString() };
fields.Add(fld);
}
}
// Add the table and fields to the dictionary
tables.Add($"{parentName}_{element.Name!}", fields);
}
}
}
}