简体   繁体   中英

Quickly read XML files and save to tables on disk with auto field mapping

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:

  1. Using c# 4.0 and .NET 4.0 and SQL server 2012 and MS Access
  2. Import xml data based on auto field mapping (field name and field type) from XML to table(s). I have XSD. just add new rows no key matching.
  3. Can override field and type mapping when desired (haven't decided whether to hard code that or use a template/mapping file so I don't have to recompile)
  4. I can duplicate field mapping if desired (1 xml field to many table fields)
  5. Low maintainability of code / setup is more important than import/transform speed – but nice to know all options if needed
  6. 3rd party solutions may be acceptable

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM