i have an XML file like this
<?xml version="1.0" encoding="utf-8" ?>
<conStr>
<server>192.168.1.25;</server>
<initial_catalog>chargdb;</initial_catalog>
<uid>sa;</uid>
<pwd>1;</pwd>
</conStr>
and i’m using this code for fetch data from this file :
XmlDocument xd = new XmlDocument();
xd.Load(Application.StartupPath + @"\cng.xml");
string conStr = string.Empty;
conStr += "server=";
conStr +=xd.DocumentElement.ChildNodes[0].Attributes["server"].Value;
conStr += "initial catalog=";
conStr += xd.DocumentElement.ChildNodes[0].Attributes["initial_catalog"].Value;
conStr += "uid=";
conStr += xd.DocumentElement.ChildNodes[0].Attributes["uid"].Value;
conStr += "pwd=";
conStr += xd.DocumentElement.ChildNodes[0].Attributes["pwd"].Value;
MessageBox.Show(conStr);
but each time i give an error message like this: “Object reference not set to an instance of an object.”
please help me, what can i do to read my file? thanks
Your existing code is looking for attributes within the
<server>element. That’s not going to work, as you should be looking for elements within the root element.I would make two changes:
SqlConnectionStringBuilderinstead of as a stringSo something like this: