Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
I have the following XML file:
<?xml version="1.0"?>
<company>
<companyname>
<group>Accenture BPO</group>
<group>Accenture IT </group>
<group>Accenture Infotech</group>
</companyname>
<companyname>HCL</companyname>
</company>
hOW would I store it in a database table ?



pleasehelp a new bea...i worked on it but didnt get success..
Posted
Comments
Aman4.net 27-Dec-10 6:10am    
Dear Shikhar,

Please do not write your question in codeblock. Only paste your code in CodeBlock.
#realJSOP 27-Dec-10 6:37am    
What application platform are you developing for? Winforms? WPF? Silverlight? ASP.Net? What language are you developing in? C#? VB? What database are you using? SQL? Access? FoxPro? Oracle? MySql? Modify your tags so you can get the help you need.

Hey Dude,

Just try it. If SQL table coloumn and xml tags doesn't have same name or above the fullfil your requirement. I hope it will help you.

DataSet reportData = new DataSet();
reportData.ReadXml(Server.MapPath("report.xml"));
SqlConnection connection = new SqlConnection("CONNECTION STRING");
SqlBulkCopy sbc = new SqlBulkCopy(connection);
sbc.DestinationTableName = "report_table";
       
//if your DB col names don’t match your XML element names 100%
//then relate the source XML elements (1st param) with the destination DB cols
sbc.ColumnMappings.Add("campaign", "campaign_id");
sbc.ColumnMappings.Add("cost", "cost_USD");
if(connection.State==ConnectionState.Closed)
{
   connection.Open();
}
//table 4 is the main table in this dataset
sbc.WriteToServer(reportData.Tables[4]);
connection.Close();


Regards!
Aman
* Don't forget to Mark Answer or comment.
 
Share this answer
 
C#
// Update database
SqlDataAdapter adp=new SqlDataAdapter("select * from tableName","your_conn_str");
SqlCommandBuilder cb = new SqlCommandBuilder(adp);
DataSet ds = new DataSet();
ds.ReadXml("file.xml");
DataTable dt = ds.Tables["tableName"];
dt.Rows.Add("val1", "val2");
// Adding row
adp.Update(ds, "tableName");
 
Share this answer
 
Comments
#realJSOP 27-Dec-10 6:34am    
With what little info we got in the question, this is the best answer so far.
Wild-Programmer 27-Dec-10 7:37am    
Thank you John :)
Follow these steps:

1. Define a column of type "XML" in your desired table.

2. Run the INSERT query along with passing the XML data as a string. For example:

SQL
INSERT INTO MyTable(Data)
Values '<?xml version="1.0"?>
<company>
<companyname>
<group>Accenture BPO</group>
<group>Accenture IT </group>
<group>Accenture Infotech</group>
</companyname>
<companyname>HCL</companyname>
</company>'


So, if you need to save the XML programmatically, you just need to set the XML as a string value before executing the INSERT statement.
 
Share this answer
 
Your question is stated so poorly that nobody can possibly give you the answer you need. You didn't mention the platform, language, or what type of database you're using, so we have to guess at those as a well.

Loading and parsing the XML file in the application is actually a very simple matter. Getting the data into a database is the tricky part, because you have to be somewhat familiar with how to write a stored procedure (assuming you're using SQL Server).

Do you want to save the entire file in the database as a single field, or do you want to save the individual fields from the file as a row of columns? Both of the answers below assume that you know hoe to open a connection to your database and call a stored procedure, and that you actually have the ability to write the necessary stored procedure.

0) To save the entire file:

XDocument doc = XDocument.Load(filename);
string myDocAsString = doc.ToString();


At this point, your entire file is represented by the string, and can be saved in a single column in your database.

1) To save each record in the XML file in your database, you have to load/parse the XML file into individual objects. The best way to do that has already been described, and for some unknown reason, voted as a 1. Look at Amit Kumar Tiwari's answer closely. A DataSet can parse XML data into DataTable objects, and then you can pass those objects to the database.

Instead of being so quick to vote answers you don't like with a 1, try giving us more info so we can help you .

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900