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.
This should work :
On your foreach statement you declare a variable
XmlNode childNode
that will refers to every node inxn2
. So you need to usechildNode
and notxn2
because the value ofxn2
never change. That's why you always get the same value