简体   繁体   中英

want to import data from XML to SQLite database

I have generated (export) an XML from my SQLite database in c#. Now, I need to import those data from the XML to an empty sqlite database.I also need to verify the contents while inserting the data into table. My generated xml from the database is -

<root>
    <name1>
        <names>
           <id>5</id>
           <class>space</class>
           <from>Germany</from>
           <to>France</to>
           <through>
              <via>
                   <id>7</id>
                   <route>Vienna<route>
               </via>
           </through>           
        </names>
        <mynames3>Black</mynames3>
        <mynames4>Hawkins</mynames4>
    </name1>
    <name2>
      <newNames>
          <id>8</id>
          <path>Road</path>
          <dest>USA</dest>
          <through>
              <route1>
                  <id>5</id>
                  <naviagte>Britain</naviagte>
                  <naviagte2>Holland</naviagte2>
                  <naviagting2>
                      <naviagtes2>
                            <naviagte5>France</naviagte5>
                            <naviagte6>US</naviagte6>
                            <naviagte7>Canada</naviagte7>
                       <naviagtes2>
                  </naviagting2>
                  <naviagte11>Russia</naviagte11>
                  <naviagte12>Poland</naviagte12>
              </route1>
              <route1>
                  <id>2</id>
                  <naviagte>Canada</naviagte>
              </route1>
          </through>              
      </newNames>
    </name2>
    <name3>H2V3</name3>
    <name4>H5V8</name4>
</root>

Could you give me any suggestion how to do so ? I am trying the following approach.

SqliteConnection sqlCon = new SqliteConnection("Data Source=" + dataPath + "/Empty.db3");
            sqlCon.Open();
            SqliteCommand sqlCmd = new SqliteCommand(sqlCon);

            DataSet ds = new DataSet();
            ds.ReadXml(Path.Combine(syncPath, tableName + "_4.xml"), XmlReadMode.ReadSchema);
            foreach(DataTable dt in ds.Tables)
            {
                //Get field names
                string sqlString = "INSERT into " + tableName + " (";
                string valString = "";
                var sqlParams = new string[dt.Rows[0].ItemArray.Count()];
                int count = 0;
                foreach(DataColumn dc in dt.Columns)
                {
                    sqlString += dc.ColumnName + ", ";
                    valString += "@" + dc.ColumnName + ", ";
                    sqlParams[count] = "@" + dc.ColumnName;
                    count++;
                }
                valString = valString.Substring(0, valString.Length - 2);
                sqlString = sqlString.Substring(0, sqlString.Length - 2) + ") VALUES (" + valString + ")";

                sqlCmd.CommandText = sqlString;
                foreach(DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < dr.ItemArray.Count(); i++) 
                    {
                        sqlCmd.Parameters.AddWithValue(sqlParams[i], dr.ItemArray[i] ?? DBNull.Value);
                    }

                    sqlCmd.ExecuteNonQuery();
                }
            }  

I need to verify the contents before inserting the data into an empty SQLite database. Could you please tell me which approach i should pursue ?

Friend, you can check this link it will help you in import and export of XML in SQLite

http://code.google.com/p/sqlite-manager/wiki/Import_Export_Files

If it helps, first generate the C# classes using there XML2CSharp website. You will deserialise into this generated class.

Next create the classes that you will need to store the deserialised data into. Create these classes as tables.

Finally you (pretty much) copy in from the deserialised data into your SQLite container classes and then store those classes.

Deserialisation is performed in 4 lines...

public static List<Jobs> JobData = new List<Jobs>();

var s = new XmlSerializer(typeof(Jobs));
var r = new StreamReader(xmlFilename);
JobData.Add((Jobs)s.Deserialize(r));
r.Close();

I'll get something onto github tonight if it helps

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