Hi I have problem in reading xml file. The things what I want to read is :

"cmpd_name","cmpdnr","rt","mz","i","a","sn","z".

Here there may be several values for each "mz","i","a","sn" in different lines under a "cmpd_name" (It will be clear if you see the attched xml file). Each of these ("mz","i","a","sn") values under a "cmpd_name" should be seperated by "," and should store in the table in a single cell.

for example that table for first "cmpd_name" should look like this:

This is a single row and different columns in the table

mz                 i                 a              sn                   z

285.13449609,    4896.99,        15532.5,2415.5    6.409,1                1
286.13775204      764.038

for this the code I have done up to my knowledge is

private void button1_Click(object sender, EventArgs e)
        {
            string key = "";

            OpenFileDialog f = new OpenFileDialog();
            if (f.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = f.FileName;
               
            }
            XmlDocument xml = new XmlDocument();
            XmlTextReader reader1 = new XmlTextReader("" + textBox1.Text + "");
            reader1.WhitespaceHandling = WhitespaceHandling.None;
            reader1.Read();
           // MessageBox.Show("ok");
            while (reader1.Read())
            {

                while (reader1.MoveToNextAttribute())
                {
                  //  MessageBox.Show("ok1");
                    if (reader1.Name == "cmpd_name")
                    {
                        textBox2.Text=reader1.Value;
                      //  MessageBox.Show("text2 = "+textBox2.Text);
                                            
                    }
                    if (reader1.Name == "mz")
                    {
                        if (key == "")
                        {
                            key = textBox2.Text;
                        }
                        if (key == textBox2.Text)
                        {
                            if (textBox3.Text == "")
                            {
                                textBox3.AppendText(reader1.Value);
                            }
                            else
                            {

                                textBox3.AppendText("," + reader1.Value);
                                
                            }
                        }
                        else
                        {
                            key = textBox2.Text;
                            textBox3.Clear();
                            textBox3.AppendText(reader1.Value);
                        }
                      //  MessageBox.Show(textBox3.Text);
                    }
                    if (reader1.Name == "i")
                    {
                        if (key == "")
                        {
                            key = textBox2.Text;
                        }
                        if (key == textBox2.Text)
                        {
                            if (textBox4.Text == "")
                            {
                                textBox4.AppendText(reader1.Value);
                            }
                            else
                            {

                                textBox4.AppendText("," + reader1.Value);
                                
                            }
                        }
                        else
                        {
                            key = textBox2.Text;
                            textBox4.Clear();
                            textBox4.AppendText(reader1.Value);
                        }
                     //   MessageBox.Show(textBox4.Text);
                    }
                    if (reader1.Name == "z")
                    {
                        if (key == "")
                        {
                            key = textBox2.Text;
                        }
                        if (key == textBox2.Text)
                        {
                            if (textBox5.Text == "")
                            {
                                textBox5.AppendText(reader1.Value);
                            }
                            else
                            {

                                textBox5.AppendText("," + reader1.Value);

                            }
                        }
                        else
                        {
                            key = textBox2.Text;
                            textBox5.Clear();
                            textBox5.AppendText(reader1.Value);
                        }
                       // MessageBox.Show(textBox4.Text);
                    }
                        
                    if (reader1.Name == "rt")
                    {
                        textBox6.Text =  reader1.Value;
                    }
                    if (reader1.Name == "cmpdnr")
                    {
                        textBox7.Text = reader1.Value;
                    }
                    if (reader1.Name == "a")
                    {
                        if (key == "")
                        {
                            key = textBox2.Text;
                        }
                        if (key == textBox2.Text)
                        {
                            if (textBox8.Text == "")
                            {
                                textBox8.AppendText(reader1.Value);
                            }
                            else
                            {

                                textBox8.AppendText("," + reader1.Value);

                            }
                        }
                        else
                        {
                            key = textBox2.Text;
                            textBox8.Clear();
                            textBox8.AppendText(reader1.Value);
                        }
                       // MessageBox.Show(textBox8.Text);
                    }
                    if (reader1.Name == "sn")
                    {
                        if (key == "")
                        {
                            key = textBox2.Text;
                        }
                        if (key == textBox2.Text)
                        {
                            if (textBox9.Text == "")
                            {
                                textBox9.AppendText(reader1.Value);
                            }
                            else
                            {

                                textBox9.AppendText("," + reader1.Value);

                            }
                        }
                        else
                        {
                            key = textBox2.Text;
                            textBox9.Clear();
                            textBox9.AppendText(reader1.Value);
                        }
                       // MessageBox.Show(textBox9.Text);
                    }
                    if (reader1.Name == "res")
                    {
                        if (key == "")
                        {
                            key = textBox2.Text;
                        }
                        if (key == textBox2.Text)
                        {
                            if (textBox10.Text == "")
                            {
                                textBox10.AppendText(reader1.Value);
                            }
                            else
                            {

                                textBox10.AppendText("," + reader1.Value);

                            }
                        }
                        else
                        {
                            key = textBox2.Text;
                            textBox10.Clear();
                            textBox10.AppendText(reader1.Value);
                        }
                      //  MessageBox.Show(textBox10.Text);
                    }
                    if (reader1.Name == "deconvoluted_molecular_mass")
                    {
                        if (key == "")
                        {
                            key = textBox2.Text;
                        }
                        if (key == textBox2.Text)
                        {
                            if (textBox11.Text == "")
                            {
                                textBox11.AppendText(reader1.Value);
                            }
                            else
                            {

                                textBox11.AppendText("," + reader1.Value);

                            }
                        }
                        else
                        {
                            key = textBox2.Text;
                            textBox11.Clear();
                            textBox11.AppendText(reader1.Value);
                        }
                        //MessageBox.Show(textBox11.Text);
                    }
                    if (key != textBox2.Text)
                    {
OdbcCommand Odbc11 = new OdbcCommand();
                        //  k = k + 1;
                        Odbc11.CommandText = "insert INTO s_Metabolite_mining (FeatureNr, Retensiontime, Charge, mzlist, Intensitylist, Rel_Intensitylist, Arealist, SNlist, Deconvolutedlist) VALUES ('" + textBox7.Text + "','" + textBox6.Text + "','" + textBox5.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "')";

                        Odbc11.Connection = OdbcCon;
                        OdbcCommand Odbc = new OdbcCommand();
                        Odbc11.Connection = OdbcCon;

                        Odbc11.ExecuteNonQuery();
                        MessageBox.Show("ok1");
                    }
                  
                    
                }
                reader1.MoveToElement();

               
            }
        }

Please help me to resolve this.

Thanks

Recommended Answers

All 2 Replies

Please attach/paste the content of XML document here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.