Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to import XML data using C# dot net to the SQL SERVER 2000.

Any one post solution for this.

I have an XML file like this below I mentioned.

HTML
- <shg_repayment_details>
- <data>
  <tally_rec_id_code>452</tally_rec_id_code> 
- <!-- To check already exit in our database 
  --> 
  <cluster_id>COICLT0001</cluster_id> 
  <unit_type>VIL</unit_type> 
  <vprc_id>COIVIL0001</vprc_id> 
  <shg_id>SHG1</shg_id> 
  <pip_no>10120aa</pip_no> 
  <name_of_the_member>BALA</name_of_the_member> 
  <type_of_loan>RF</type_of_loan> 
  <principal>500</principal> 
  <interest>600</interest> 
  </data>
- <data>
  <tally_rec_id_code>453</tally_rec_id_code> 
- <!-- To check already exit in our database 
  --> 
  <cluster_id>COICLT0001</cluster_id> 
  <unit_type>VIL</unit_type> 
  <vprc_id>COIVIL0001</vprc_id> 
  <shg_id>SHG2</shg_id> 
  <pip_no>10120BB</pip_no> 
  <name_of_the_member>BALAJI</name_of_the_member> 
  <type_of_loan>DL</type_of_loan> 
  <principal>300</principal> 
  <interest>500</interest> 
  </data>
  </shg_repayment_details>
Posted
Updated 29-Nov-11 21:59pm
v5
Comments
Dalek Dave 30-Nov-11 3:59am    
Edited for Code Block

Well, you need to parse XML using one the ways implemented in standard .NET libraries. Here is my short overview:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].
—SA
 
Share this answer
 
Hi if you want to insert a XML text in SQL 2000 you can see this link[^]

but if you want to send a XML text to SQL by C#, this work is very easy:
Multiple CDATA elements are not consistantly supported across implementations. For example, you will have problems accessing them an XDocument or via SelectNodes. If you can change the input format that would make things easier.

This code hasn't been tested and there's no error handling or bad data checking, but it should get you started. Investigate using XPathDocument / XPathNavigator for performance and read my inline comments.


C#
class XmlCsvImport
{
    public void ImportData(string xmlData, ConnectionStringSettings connectionSettings)
    {
        DbProviderFactory providerFactory = DbProviderFactories.GetFactory(connectionSettings.ProviderName);

        IDbConnection connection = providerFactory.CreateConnection();
        connection.ConnectionString = connectionSettings.ConnectionString;

        // TODO: Begin transaction

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlData);

        foreach (XmlNode tableNode in doc.SelectNodes("/transaction/table"))
        {
            IDbCommand command = CreatCommand(connection, tableNode);

            foreach (XmlNode rowNode in tableNode.SelectNodes("data/row"))
            {
                string[] values = GetRowValues(rowNode);

                if (values.Length != command.Parameters.Count)
                {
                    // TODO: Log bad row
                    continue;
                }

                this.FillCommand(command, values);
                command.ExecuteNonQuery();
            }
        }

        // TODO: Commit transaction
    }

    private IDbCommand CreatCommand(IDbConnection connection, XmlNode tableNode)
    {
        string tableName = tableNode.Attributes["name"].Value;

        IDbCommand command = connection.CreateCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;

        XmlNodeList fieldNodes = tableNode.SelectNodes("fields/field");

        List<string> fieldNameList = new List<string>(fieldNodes.Count);

        foreach (XmlNode fieldNode in tableNode.SelectNodes("fields/field"))
        {
            string fieldName = fieldNode.Attributes["name"].Value;
            int fieldType = Int32.Parse(fieldNode.Attributes["type"].Value);
            int fieldSize = Int32.Parse(fieldNode.Attributes["size"].Value);

            IDbDataParameter param = command.CreateParameter();
            param.ParameterName = String.Concat("@", fieldNode.Attributes["name"]);
            param.Size = fieldSize;
            param.DbType = (DbType)fieldType; // NOTE: this may not be so easy
            command.Parameters.Add(param);

            fieldNameList.Add(fieldName);
        }

        string[] fieldNames = fieldNameList.ToArray();

        StringBuilder commandBuilder = new StringBuilder();
        commandBuilder.AppendFormat("INSERT INTO [{0}] (", tableName);

        string columnNames = String.Join("], [", fieldNames);
        string paramNames = String.Join(", @", fieldNames);

        command.CommandText = String.Concat(
            "INSERT INTO [", tableName, "] ([",
            columnNames,
            "]) VALUES (@",
            paramNames,
            ")"
            );

        return command;
    }

    private string[] GetRowValues(XmlNode row)
    {
        List<string> values = new List<string>();

        foreach (XmlNode child in row.ChildNodes)
        {
            if (child.NodeType == XmlNodeType.Text ||
                child.NodeType == XmlNodeType.CDATA)
            {
                values.Add(child.Value);
            }
        }

        return values.ToArray();
    }

    private void FillCommand(IDbCommand command, string[] values)
    {
        for (int i = 0; i < values.Length; i++)
        {
            IDbDataParameter param = (IDbDataParameter)command.Parameters[i];
            param.Value = values[i]; // TODO: Convert to correct data type
        }
    }
</string></string></string></string>
 
Share this answer
 
v2
Comments
Dalek Dave 30-Nov-11 4:00am    
Good Answer.
Sergey Alexandrovich Kryukov 30-Nov-11 12:19pm    
Look at the code: formatting is messed up. It happens if your forget to escape <> in "<string>", etc. When you edit the text, pay attention to "encode" at the top of text box -- it happens you to escape HTML characters with HTML entities.
--SA
Member 10546376 5-Feb-14 6:02am    
how to import and export xml code in c.pls give solution with example

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