简体   繁体   English

想要将数据从 XML 导入 SQLite 数据库

[英]want to import data from XML to SQLite database

I have generated (export) an XML from my SQLite database in c#.我已经在 c# 中从我的 SQLite 数据库生成(导出)一个 XML。 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.现在,我需要将这些数据从 XML 导入到一个空的 sqlite 数据库。我还需要在将数据插入表时验证内容。 My generated xml from the database is -我从数据库生成的 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>

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.在将数据插入空的 SQLite 数据库之前,我需要验证内容。 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 朋友,您可以检查此链接,它将帮助您在SQLite中导入和导出XML

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

If it helps, first generate the C# classes using there XML2CSharp website.如果有帮助,请首先使用 XML2CSharp 网站生成 C# 类。 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.最后,您(几乎)从反序列化数据复制到 SQLite 容器类中,然后存储这些类。

Deserialisation is performed in 4 lines...反序列化在 4 行中执行...

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如果有帮助,我今晚会在 github 上得到一些东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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