XML Upload
-
Hi, I need to read the contents of a xml file and upload them into a database. Do we have any xml specific class which could be used in this scenario. Thanks in Advance:
Rakesh
Using XmlDocument you can load your xml file. The namespace would be System.Xml You can use the Xml Path to acces your nodes in your xml file accordingly. To see more examples on xpath refer w3schools.com
Gautham
-
Using XmlDocument you can load your xml file. The namespace would be System.Xml You can use the Xml Path to acces your nodes in your xml file accordingly. To see more examples on xpath refer w3schools.com
Gautham
Thanks for the response I am already using XMLDocument class. Let me tell you what exactly I am doing: I am reading one set of record of an xml file and opening a SQL connection and inserting the data and then closing the connection. If I have 100 set of records in the XML file then my application opens connection 100 times which I dont see appropriate. I want to do it at one short. Read all the sets from the xml file, open the connection and insert all the records and close it. Do we have any way to do this?
Rakesh
-
Thanks for the response I am already using XMLDocument class. Let me tell you what exactly I am doing: I am reading one set of record of an xml file and opening a SQL connection and inserting the data and then closing the connection. If I have 100 set of records in the XML file then my application opens connection 100 times which I dont see appropriate. I want to do it at one short. Read all the sets from the xml file, open the connection and insert all the records and close it. Do we have any way to do this?
Rakesh
-
Hi, I need to read the contents of a xml file and upload them into a database. Do we have any xml specific class which could be used in this scenario. Thanks in Advance:
Rakesh
I think its very easy to use the Dataset method.. ie use the namespace System.Data then code as follows Dataset Ds; ds.ReadXml(xmlfilepath); here if you check the dataset viewer, you can see all the xml contents as tables. from there you can extract data and put it in database.
-
I think its very easy to use the Dataset method.. ie use the namespace System.Data then code as follows Dataset Ds; ds.ReadXml(xmlfilepath); here if you check the dataset viewer, you can see all the xml contents as tables. from there you can extract data and put it in database.
-
Why are you opening and closing the connection? Why not open the connection and command object, loop through the records in the XML file building the SQL on the fly each time and runnning the query. Only close the connection once your done.
Yah thats really a simple idea. But I see one issue here. Let say that there are 100 sets of employee records in the xml file and the process finished inserting 60 of the records and suddenly the connection is lost, then what must be done. I mean how can we track the record to be inserted next. Thanks:
Rakesh
-
I think its very easy to use the Dataset method.. ie use the namespace System.Data then code as follows Dataset Ds; ds.ReadXml(xmlfilepath); here if you check the dataset viewer, you can see all the xml contents as tables. from there you can extract data and put it in database.
I have implemented your suggestion, but facing some issues: Below is the code I have used: protected void Button1_Click(object sender, EventArgs e) { string path = @"D:\emp.xml"; string dsTable = ""; string connectionString = @"Server = DB\SQL2; Database = CWBDB; user = sa; password = sa"; DataSet ds = new DataSet("EMPDS"); ds.ReadXml(path); foreach (DataTable t in ds.Tables) { dsTable = t.ToString(); } SqlConnection con = new SqlConnection(connectionString); SqlDataAdapter da = new SqlDataAdapter(); da.TableMappings.Add("EMP", dsTable); if ((da.Update(ds))>0) { Label1.Text = "Uploaded the data successfully"; } else { Label1.Text = "Could not Upload the data"; } } But I see an exception: "Update unable to find TableMapping['Table'] or DataTable 'Table'." Please let me know what needs to be done for this!
Rakesh