Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to compare data in xml file with data in database. Please help me.
here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;
using System.Reflection;
using System.CodeDom;
using System.Collections.Specialized;

namespace WindowsFormsApplication8try
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

       
             private void button1_Click(object sender, EventArgs e)
        {
            XmlWriter writer = XmlWriter.Create(@"C:\SIEM\xml.xml");

            writer.WriteStartElement("QUERY");

            writer.WriteStartElement("IT");
            writer.WriteAttributeString("macName", textBox1.Text);
            writer.WriteElementString("Generated_source", textBox2.Text);
            writer.WriteElementString("TimeStemp", textBox3.Text);
            writer.WriteEndElement();
            
      writer.Close();}

             private void button2_Click(object sender, EventArgs e)
             {

                 string myXMLfile = @"C:\SIEM\xml.xml";
                 DataSet ds = new DataSet();
                 // Create new FileStream with which to read the schema.
                 System.IO.FileStream fsReadXml = new System.IO.FileStream
                     (myXMLfile, System.IO.FileMode.Open);
                 try
                 {
                     //nchar(10) Source;

                     //string Machine_Name = null;

                     //double Time_stemp = 0;
                     //DataSet ds = new DataSet();

                     ds.ReadXml(fsReadXml);
                     DataTable dbTable1 = ds.Tables[0];
                     connString = 
                     DataTable dt = new DataTable("query_table");

DataColumn col1 = new DataColumn("Machine_Name");

DataColumn col2 = new DataColumn("Source_generated");

DataColumn col3 = new DataColumn("Time_stemp");


dt.Columns.Add(col1);

dt.Columns.Add(col2);

ds.Tables.Add(dt);

//Add Employee Data to the table

//Create a new row and add the data to the corresponding columns

//DataRow dr; dr = dt.NewRow();

//dr ["Name"] = "Rajesh";

//dr ["Address"] = "Hagley Road";

//dt.Rows.Add(dr);

//dr = dt.NewRow();

//dr ["Name"] = "Tim Easton";

//dr ["Address"] = "London";

//dt.Rows.Add(dr);

//dr = dt.NewRow();

//dr ["Name"] = "Sara";

//dr ["Address"] = "Oxford";

//// Now add the datarows to the datatable

//dt.Rows.Add(dr);

// After adding the rows to the datatable use AcceptChanges method if there are any changes

ds.AcceptChanges();
                     dataGridView1.DataSource = dbTable1;
                    //ds.ReadXml("Xml.xml");
                     string myConnectionString ="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Documents\Downloads\WindowsFormsApplication8try\Database1.mdf;Integrated Security=True;User Instance=True";
                      SqlConnection myConnection = new SqlConnection(myConnectionString);
                     
                     string query = "insert into query_table values(" + Machine_Name + ",'" + Source + "'," + Time_stemp + ")";
                     DataAdapter objAdapter = new DataAdapter(query,myConnectionString);
                    
                     objAdapter.Fill(ds);

                     //          for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)

                     //{

                     //    Machine_Name = ds.Tables[0].Rows[i].ItemArray[0];

                     //    Source = ds.Tables[0].Rows[i].ItemArray[1].ToString();

                     //   Timestemp = ds.Tables[0].Rows[i].ItemArray[2];
                    // DataTable query_table = new DataTable();
                     

                     // string sqlcommand = "insert into query_table values(" + Machine_Name + ",'" + Source + "'," + Time_stemp + ")";

                    // sqlcommand = new SqlCommand(sqlcommand, connection);

                    //objAdapter.InsertCommand = command;

                    // query_tableTableAdapter.InsertCommand.ExecuteNonQuery();



                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.ToString());
                 }
                 finally
                 {
                     fsReadXml.Close();
                 }
             }
                 
             
             private void Form1_Load(object sender, EventArgs e)
             {
                 System.Data.SqlClient.SqlDataAdapter query_tableTableAdapter = new SqlDataAdapter();
                 //System.Data.SqlClient.SqlDataAdapter query_tableTableAdapter = new SqlDataAdapter();
                 //  This line of code loads data into the 'database1DataSet.query_table' table.
                 query_tableTableAdapter.Fill(this.database1DataSet1.query_table);
                 // This line of code loads data into the 'dataSet1.Credit' table. 

                // query_tableTableAdapter.Fill(this.database1DataSet1.query_table);


             }

        
    }}


i know it isn't well written but my initial problem is:it not save my xml data in database,then its further work
Posted
Updated 18-Oct-10 8:26am
v4
Comments
Khaniya 12-Oct-10 2:13am    
be more specific
show up your database structure, XML file data sample
Sandeep Mewara 12-Oct-10 3:46am    
Ok. Go ahead and do it. Whats stopping you? Issue?
jia123m 12-Oct-10 6:56am    
@ Sandeep Mewara yea issues my code isn't working properly :(
Rutvik Dave 18-Oct-10 11:34am    
OK, In that case, you need to post the part of your code, which is causing problem. so that people here can help you.

otherwise as you can see, everybody will think that you don't want to do anything, and just looking for code. and you will not get any help.
Toli Cuturicu 18-Oct-10 12:31pm    
Ok. But where is the actual question?

1 solution

All you have to do is,

1. Read the XML file in DataSet called dataset1.
2. Fetch data from the database into DataSet called dataset2.
3. Now compare the two DataSet, dataset1 and dataset2. You need to use a loop and then go through each rows in each DataSet and find the difference.

If you dont know how to read a XML file into DataSet, then refer to the following links.

Example 1[^]

Example 2[^]
 
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