Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import XML to database?

Tags:

c#

sqlite

xml

I exported to XML from SQLite in C#. I need to import this XML to an empty SQLite database.I also need to verify contents while inserting. My XML:

<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>

How to do so ? I tried:

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();
                }
            }
like image 263
rzmuc Avatar asked Jul 04 '26 16:07

rzmuc


1 Answers

First generate the C# classes using the XML2CSharp website. De-serialise into this generated class. Next create the classes to store the de-serialised data into. Create these as tables. Finally copy from de-serialised data into SQLite container classes and then store those classes:

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();
like image 158
Nodoid Avatar answered Jul 07 '26 05:07

Nodoid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!