How to quickly read XML files and save to a strongly typed tables on disk with auto field mapping (MS Access or SQL server 2012).
Ideas / wants:
Will this method be fast enough or is there a better method?
Step 1: Read xml into a class ( c# class was auto generated from XSD.exe tool)
ser = new XmlSerializer(typeof(MyTypeData));
MyTypeData data;
using (XmlReader reader = XmlReader.Create(XMLFullPath))
{
data = (MyTypeData)ser.Deserialize(reader);
}
Step 2: Save class to a strongly typed DataSet DataTable (in-memory table) using generics somehow (like method below)
public static DataTable CreateEmptyDataTable(MyTypeData data)
// Creates empty DataTable
{
DataTable dt = new DataTable();
foreach (PropertyInfo info in data.GetProperties())
{
dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}
return dt;
}
Step 3: Populate table on disk where DataTable field names are same as XML names where field types are the same. Skip xml element names not in table. Just add new rows and do not worry about key matching for now. Update rows based on key matching could be added later.
Step 4: Add custom source to dest field mapping : (how - what is best way?)
In case you have the xsd files from the xml files, you might want to try xsd2code which is a codeplex project. See here . This makes classes within your code based on the Xsd format.
For me this allowed me to create xml code very easily with very few problems. When the xml scheme changes you can just update the code to generate the new classes.
Very nice and neat, although, this is ofcourse personal preference.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.