我有一个包含30个表的.xml
文件,我希望读取所有表并将数据插入SQL Server数据库而不声明任何变量,这意味着一切都是动态发生的
<person>
<created_by>admin</created_by>
<created_on_timestamp>2014-11-19T14:13:54.000Z</created_on_timestamp>
<date_of_birth>1990-04-04</date_of_birth>
<last_modified_by>admin</last_modified_by>
<last_modified_on>2014-11-21T13:29:49.000Z</last_modified_on>
<logon_user_id>P10621</logon_user_id>
<logon_user_is_active>true</logon_user_is_active>
<logon_user_name>P10621</logon_user_name>
<person_id>3478</person_id>
<person_id_external>P10621</person_id_external>
<personal_informatio>
<last_modified_by>admin</last_modified_by>
<last_modified_on>2014-11-21T14:45:49.000Z</last_modified_on>
<last_name>Singh</last_name>
<marital_status>S</marital_status>
<nationality>IND</nationality>
<salutation>MR</salutation>
<start_date>2014-05-01</start_date>
<personal_information_ind>
<country>IND</country>
<created_by>admin</created_by>
<created_on_timestamp>2014-11-21T13:50:33.000Z</created_on_timestamp>
<custom_string1>hi</custom_string1>
<genericNumber1>22</genericNumber1>
<address_information>
<address1>-0</address1>
<address2>Villa -342,Omax Panorama City</address2>
<address3>-0</address3>
<address4>manphoollamori@gmail.com</address4>
<address5>919828513833</address5>
<address_type>home</address_type>
<city>Bhiwadi</city>
<country>IND</country>
<county>India</county>
<created_by>admin</created_by>
<created_on_timestamp>2014-11-21T15:09:41.000Z</created_on_timestamp>
<end_date>9999-12-31</end_date>
<is_global_model_address>false</is_global_model_address>
<last_m
答案 0 :(得分:0)
XmlDocument doc = new XmlDocument();
doc.Load("yourxmlfile.xml");
string databasename = doc.DocumentElement.Name;
foreach (XmlNode node in doc.SelectNodes("/" + databasename + "/*[starts-with(name(), 'SourceTableName')]"))
{
string tablename = node.Attributes["targetTable"].Value;
string Columns = "";
string Values = "";
foreach (XmlNode field in node.SelectNodes("Field"))
{
Columns += (!string.IsNullOrEmpty(Columns) ? ", " : "") + field.Attributes["targetField"].Value;
Values += (!string.IsNullOrEmpty(Values) ? ", " : "") + "'" + field.InnerText + "'";
}
//Generate insert statement
string statement = string.Format("Insert Into {0}.{1} ({2}) Values ({3})",
databasename,
tablename,
Columns,
Values);
Console.WriteLine(statement);
}