I want to export xml file data to sql database table. Can anyone guide me for this?
+2
A:
If it's an SQL Server, I already answered a similar Question. Have a look at the following posting:
You can use that small c# part to store your data. You only have to amend the table and column fields.
class Program
{
private static void SaveXmlToDatabase(DbConnection connection,
XmlDocument xmlToSave)
{
String sql = "INSERT INTO xmlTable(xmlColumn) VALUES (@xml)";
using (DbCommand command = connection.CreateCommand())
{
XPathNavigator nav = xmlToSave.CreateNavigator();
string xml = nav.SelectSingleNode("/catalog/cd[title='Manowar']").InnerXml;
command.CommandText = sql;
command.Parameters.Add(
new SqlParameter("@xml", SqlDbType.Xml)
{Value = new SqlXml(new XmlTextReader(xml
, XmlNodeType.Document, null)) });
DbTransaction trans = connection.BeginTransaction();
try
{
command.ExecuteNonQuery();
trans.Commit();
}
catch (Exception)
{
trans.Rollback();
throw;
}
}
}
static void Main(string[] args)
{
XmlDocument document = new XmlDocument();
document.Load(args.First());
SqlConnection connection = new SqlConnection(
"Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;");
SaveXmlToDatabase(connection, document);
connection.Close();
}
}
BitKFu
2010-08-30 06:17:48
Is it possible to filter the fields from the xml file while passing it as the parameter to insert?
Geetha
2010-08-30 09:35:11
you could add a xpath query to filter the xml file.
BitKFu
2010-08-30 10:56:53
Can you please explain a bit more please.
Geetha
2010-08-30 10:59:36
Yes, you can do that with the XPathNavigator for example.XPathNavigator nav = xmlToSave.CreateNavigator();string xml = nav.SelectSingleNode("/catalog/cd[title='Manowar']").InnerXml;
BitKFu
2010-08-30 13:44:45
+2
A:
check below link for this
http://www.simple-talk.com/sql/t-sql-programming/beginning-sql-server-2005-xml-programming/
you can find the solution.
KuldipMCA
2010-08-30 06:23:24