There is a C # application that processes data from xml. There is xml and the corresponding xsd scheme. Is it possible to automatically create a database (ms sql 2008 or ms sql compact) with a table for storing information from xml and upload data there?
- @ e1s, If you are given a comprehensive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina
|
1 answer
Immediately the thought arises of deserializing XML into an object. Then, using reflection, run through all the properties of the object and generate the sql script
var student = new Student(); foreach (PropertyInfo propertyInfo in student.GetType().GetProperties()) { Console.WriteLine(propertyInfo.Name); Console.WriteLine(propertyInfo.PropertyType); } Based on the properties, it is easy to select the required data type in the database and create a script .
Here is a hastily scribbled extension method for generating a script:
using System; using System.Reflection; using System.Text; namespace DynamicCreteTable { public static class ExtensionCreateScript { public static string CreateTableScript(this object obj ) { StringBuilder builder = new StringBuilder(); builder.Append("CREATE TABLE "); builder.Append(obj.GetType().Name); builder.Append("("); foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties()) { builder.Append(propertyInfo.Name); builder.Append(" "); if (propertyInfo.PropertyType==typeof(Int32)) { builder.Append("int, "); } if (propertyInfo.PropertyType==typeof(string)) { builder.Append("varchar(255), "); } } builder.Remove(builder.Length - 2, 2); builder.Append(");"); return builder.ToString(); } } } - And why then need xsd scheme? - e1s
- stackoverflow.com/questions/1009154/… here they suggest using ready-made software - beliy26rus
- as I understand, this utility does not work with sql compact - e1s
|