Hi Guys , I am trying to load a xml file as follows and i am getting file not found exception even though the file is on the desktop.I have checked the file name and extension they are correct , can anyonehelp please

private void button1_Click(object sender, EventArgs e)
        {  string myString;
            string sXMLFilePath = @"C:\\Dict0.xml";
            XmlDocument document = new XmlDocument();
           
           document.Load(sXMLFilePath);
           XmlNodeList nodelist = document.SelectNodes("string");
           SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
           SqlDataAdapter da = new SqlDataAdapter();

           foreach (XmlNode node in nodelist)

               //cn.ConnectionString = "Server=server;Database=RTLUser;UID=sa;";
               cn.Open();
           myString = @"INSERT INTO wordTbl(word,level)  Values('" + nodelist + "','" + "0" + "')";

           SqlCommand myCmd = new SqlCommand(myString, cn);
           myCmd.ExecuteNonQuery();

           cn.Close();

        }
    }

sdhawan,

if you are trying to access a file on the desktop, then the error lies in your code. Your current file location is pointing directly at the root of the C drive, not the desktop. If the file is in fact in the root of C, then there is another issue.

ya thanks for the reply i did figure it out. I am also getting an exception error that ExecuteNonQuery: CommandText property has not been initialized.Do you have any idea why am i getting this error .Thanks

My best bet, is that it is because you are using the nodelist variable instead of the node.innerText, so it's not properly creating the string...

other than that I have no idea...Have you tried setting a break point inside your foreach to make sure that all variables are properly being created?

I got it
I was not usinf the brackets after for each loop.ITs working now .It is suppose to be like this.

string myString = "";
            string sXMLFilePath = "C:\\Dict1.txt";
            XmlDocument document = new XmlDocument();

            document.Load(sXMLFilePath);
            XmlNodeList nodelist = document.SelectNodes("ArrayOfString/string");
            SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
            SqlDataAdapter da = new SqlDataAdapter();

            cn.Open();
            foreach (XmlNode node in nodelist)
            {
                string coll = node.InnerText.Replace("'","''");
                myString = @"INSERT INTO levelTbl(word,level)  Values('" + coll + "','" + "1" + "')";

                SqlCommand myCmd = new SqlCommand(myString, cn);
                myCmd.ExecuteNonQuery();

            }
This question has already been answered. Start a new discussion instead.