A System.NullReferenceObject exception is cast when I try to read the following XML:
<Settings>
<Shortcuts>
<Shortcut_1>CTRL+ALT+1</Shortcut_1>
<Shortcut_2>CTRL+ALT+2</Shortcut_2>
<Shortcut_3>CTRL+ALT+3</Shortcut_3>
<Shortcut_4>CTRL+ALT+4</Shortcut_4>
<Shortcut_5>CTRL+ALT+5</Shortcut_5>
<Shortcut_6>CTRL+ALT+6</Shortcut_6>
<Shortcut_7>CTRL+ALT+7</Shortcut_7>
<Shortcut_8>CTRL+ALT+8</Shortcut_8>
<Shortcut_9>CTRL+ALT+9</Shortcut_9>
<Shortcut_10>CTRL+ALT+10</Shortcut_10>
</Shortcuts>
<Other>
<Windows_start>true</Windows_start>
<Report_bugs>false</Report_bugs>
</Other>
</Settings>
Using the following code:
public static string Read(string read)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(AppDomain.CurrentDomain.BaseDirectory + "Settings.xml");
XmlNode node = doc.DocumentElement.SelectSingleNode(read);
//The line below causes the error.
string contents = node.InnerText;
return contents;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return "";
}
}
My intent is to read the Windows_start tag and the Report_bugs tag from the XML file using the above method.
No comments:
Post a Comment