0

Hi I have a xml in my Windows forms application. I am tring to get a node value. It is pretty basic and a lot of ways to get it I know but the wuestion is my xml not even loading. When I debug, after this line

xml.LoadXml(xmlPath);

program refuses to go to next line at all.Here is my full code.

XmlDocument xml = new XmlDocument();
string xmlPath = "settings.xml";
xml.LoadXml(xmlPath);
txtPass.Text = xml.SelectSingleNode("settings/user-settings/pass").InnerText.ToString();

Edit : I also tried xml.Load() but I have the same issue.

MCanSener
  • 53
  • 9
  • Well when you debug you go next line with f10 or f11 right? It is not going to next line at all. It fires the application form to screen. – MCanSener Feb 12 '14 at 08:52
  • Maybe the file is in use by another program exclusively? – Tarec Feb 12 '14 at 08:55
  • No it is not I checked. – MCanSener Feb 12 '14 at 08:59
  • Copy and paste that example into other project (console app) and check if it runs properly. – Tarec Feb 12 '14 at 09:03
  • When it "doesn't go to next line" it throws an exception and defaults to the UI thread, which is why the form is shown. If you were to put the load xml line in a try/catch block, you'd see it throws an exception. – Dmitriy Khaykin Feb 12 '14 at 09:07

2 Answers2

1

That's because LoadXml is not meant to load files but XML content.

Source

Tarec
  • 3,268
  • 4
  • 30
  • 47
0

Xml file name is "CompSpecs"

{
                        string path = @"";
                        FileStream READER = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //Set up the filestream (READER) //
                        System.Xml.XmlDocument CompSpecs = new System.Xml.XmlDocument();// Set up the XmlDocument (CompSpecs) //
                        CompSpecs.Load(READER); //Load the data from the file into the XmlDocument (CompSpecs) //
                        System.Xml.XmlNodeList NodeList = CompSpecs.GetElementsByTagName("CompSpecs"); 
                        textBox1.Text = NodeList[0].FirstChild.NextSibling.ChildNodes[0].InnerText; 
}

My XML FILE IS

<?xml version="1.0" ?> 
- <CompSpecs>
- <CP>
  <Type>intel Hp Core2DU</Type> 
  <RAM>4GBszz</RAM> 
  <CPU_Speed>3.8Ghz</CPU_Speed> 
  </CP>
- <CP>
  <Type>intel dell</Type> 
  <RAM>4GB</RAM> 
  <CPU_Speed>3.8Ghz</CPU_Speed> 
  </CP>
  </CompSpecs>
AHMAD SUMRAIZ
  • 535
  • 8
  • 21