How to Get more than one Childnode innertext using C#?

679 views Asked by At

I am reading XML file and storing in a table, but unfortunately I am not getting all the child node innertext values, please find my xml file and csharp coding and suggest solution.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:ce="www.dummy.com" xmlns:mml="www.dummy.com">
<head>
<title></title>
</head>
<body>
<queries>
<query><label>1</label>Please check the first hierarchy.<page>1</page></query>
<query><label>2</label>Please check the second hierarchy.<page>12</page></query>
<query><label>3</label>Please check the third hierarchy.<page>13</page></query>
</queries>
</body>
</root>

Csharp Coding

XmlNodeList xnList2 = xml.SelectNodes("/root/body/queries");
            foreach (XmlNode xn2 in xnList2)
            {
                foreach (XmlNode childNode in xn2.ChildNodes)
                {

                    queries =xn2["query"].InnerText;
                    // text should return only first query text, but I need all the query text
                    query_string = "INSERT INTO Customer_Queries values ('"+ queries + "')";
                    SqlConnection myConnection = new SqlConnection(constr);
                    myConnection.Open();
                    SqlCommand myCommand = new SqlCommand(query_string, myConnection);
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();

                }

I don`t know where I am doing wrong in code.

the first query text is repeatedly inserting for all the three rows

Please check the first hierarchy.
1

There are 1 answers

6
Guillaume Beauvois On BEST ANSWER

This should work :

    XmlNodeList xnList2 = xml.SelectNodes("/roor/body/queries");
                            foreach (XmlNode xn2 in xnList2)
                            {
                                foreach (XmlNode childNode in xn2.ChildNodes)
                                {

                                    queries = childNode.InnerText;
                                    // text should return only first query text, but I need all the query text
                                    query_string = "INSERT INTO Customer_Queries values (@query)";
                                    using(SqlConnection myConnection = new SqlConnection(constr))
                                    {
                                        myConnection.Open();
                                        using(SqlCommand myCommand = new SqlCommand(query_string, myConnection))
                                        {
                                           myCommand.parameters.AddWithValue("@query", queries);
                                           myCommand.ExecuteNonQuery();
                                        }
                                    }        
                                }
                            }

On your foreach statement you declare a variable XmlNode childNode that will refers to every node in xn2. So you need to use childNode and not xn2 because the value of xn2 never change. That's why you always get the same value