none
xml parser RRS feed

  • Question

  • i have a problem when i'm run my c# code;i want to store data from xml file to sql server database after loading xml file ,but nothing happen when the code is run(the message after the code is run displayed,but when i'm return to database i found null values) i don't know what the problem.can anyone help me!!!here is the code:

     private static void parse()
            {
                try
                {
                    SqlConnection cn = new SqlConnection();
                    SqlCommand cm = new SqlCommand();
                    cm.Connection = cn;
                    SqlDataAdapter dr = new SqlDataAdapter();
                    string connetionString = null;
                    connetionString = @"Data Source=.\sqlexpress;Initial Catalog=Radar_Sim;Integrated Security=True;Pooling=False";
                    string str = "insert into vio(ID,speed,direction,vclass,frametime,text)values(@ID,@speed,@direction,@vclass,@timems,@text)";
                    XDocument doc1 = XDocument.Load(@"D:\final year\project\sample.xml");
                    IEnumerable<XElement> vio = doc1.Root.Descendants("Capture");
                    cn = new SqlConnection(connetionString);
                    foreach (XElement xe in vio)
                    {
                        long ID = Convert.ToInt64(xe.Element("ID").Value);
                        float speed = Convert.ToInt32(xe.Element("speed").Value);
                        byte direction = Convert.ToByte(xe.Element("direction").Value);
                        DateTime frametime = Convert.ToDateTime(xe.Element("frametime").Value);
                        byte vclass = Convert.ToByte(xe.Element("vclass").Value);
                        string text = xe.Element("text").Value;
                        cm = new SqlCommand(str, cn);
                        cn.Open();
                        cm.Parameters.AddWithValue("@ID", ID);
                        cm.Parameters.AddWithValue("@speed", speed);
                        cm.Parameters.AddWithValue("@direction", direction);
                        cm.Parameters.AddWithValue("@vclass", vclass);
                        cm.Parameters.AddWithValue("@frametime", frametime);
                        cm.Parameters.AddWithValue("@text", text);
                        cn.Open();
                        cm.ExecuteNonQuery();
                        cn.Close();
                    }
                        MessageBox.Show("file parsed");
                    }
               

                catch (Exception e)
                {
                   
                    MessageBox.Show(e.Message);
                }
            }
            private void button1_Click(object sender, EventArgs e)
            {
                parse();}

    Friday, May 2, 2014 1:41 PM

Answers