Im trying to read xml into SQL and get an error at the “ds.ReadXml(“C:\test.xml”);” line,
any hints as to whats wrong? Thanks
tring connectionString = "Data Source=MyServer\\SQLEXPRESS;Initial Catalog=MyCatalog;Password=xxx;Persist Security Info=True;User ID=xxx";
DataSet ds = new DataSet();
DataTable sourcedata = new DataTable();
ds.ReadXml("C:\test.xml");
sourcedata = ds.Tables[0];
using (SqlConnection sqlconn = new SqlConnection(connectionString))
{
sqlconn.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlconn))
{
bulkCopy.ColumnMappings.Add("ID", "ID");
bulkCopy.ColumnMappings.Add("user_id", "user_id");
bulkCopy.ColumnMappings.Add("upload_date", "upload_date");
bulkCopy.ColumnMappings.Add("userAddr", "userAddr");
bulkCopy.ColumnMappings.Add("serial_no", "serial_no");
bulkCopy.ColumnMappings.Add("Mfgr", "Mfgr");
bulkCopy.ColumnMappings.Add("model", "model");
bulkCopy.ColumnMappings.Add("description", "description");
bulkCopy.ColumnMappings.Add("type", "type");
bulkCopy.ColumnMappings.Add("code", "code");
bulkCopy.ColumnMappings.Add("comments", "comments");
bulkCopy.ColumnMappings.Add("qty", "qty");
bulkCopy.ColumnMappings.Add("condition", "condition");
bulkCopy.ColumnMappings.Add("location", "location");
bulkCopy.ColumnMappings.Add("price", "price");
bulkCopy.DestinationTableName = "Upload";
bulkCopy.WriteToServer(sourcedata);
}
}
}
Your
C:\test.xmlis being read asC:(tab character)est.xml. Put a @ in front of your string:@"C:\test.xml", or escape it with"C:\\test.xml".9 times out of ten, you’d get an error message along the lines of ‘illegal escape character’ when you make this mistake and it would be a lot more obvious what’s wrong, but in this case it just happens to be legal.