我已經從c#中的SQLite數據庫生成(導出)XML。現在,我需要將這些數據從XML導入到一個空的sqlite數據庫中。我還需要在將數據插入到表中時驗證內容。我從數據庫生成的XML是 -想要將數據從XML導入到SQLite數據庫
<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>
你能給我任何建議怎麼做嗎?我正在嘗試以下方法。
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();
}
}
我需要在將數據插入空的SQLite數據庫之前驗證內容。你能告訴我我應該追求哪種方法嗎?
我需要以編程方式做它在C#。請給我任何提示,如果你有任何我的上述結構的XML。 – rzmuc