Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xml config file

Status
Not open for further replies.

prrm333

Programmer
Apr 14, 2003
97
US
I can get the following code to retrieve nodes from an xml file when I click the button:

private void button1_Click(object sender, EventArgs e)
{
XmlDocument document = new XmlDocument();
document.Load(@"c:\config.xml");
XmlNode node = document.DocumentElement.SelectSingleNode("//title");
textBox8.Text = node.InnerText;
}

However I can see how to use the above code and have it load on program execution where information can then be put into a variable to populate parameters like, filename, path, etc.

Any help is appreciated.
 
What you need to do is put your code into a method that is called when your form loads

create the method ...
Code:
private string getTitle()
{
   string _title="";
   XmlDocument document = new XmlDocument();
   document.Load(@"c:\config.xml");
   XmlNode node = document.DocumentElement.SelectSingleNode("//title");
   _title = node.InnerText.ToString();

  return _title;
}

...and call it from your form load event

Code:
private void form1_load()
{
  string _title = getTitle();
  textBox8.Text = _title;
}

Patrick

 
if these values are constant then I would load an object graph with this information when the application start and store this information in memory. that way you only read the file once when the app is first loading.

then the object is a full class domain object and can be manipulated in any way you require.

An example is SystemSettings.
I would have a SystemSettings object which contains properties specific to the application.
I would also create a SystemSettingsRepository this object would load the xml file and populate a SystemSettings object.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I was able to get what I needed out of the following function:
private string getTitle()

However I am trying to build the connection string using the following XML:
<conn1>Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\calc;</conn1>
<conn2>Extended Properties=;""text;HDR=YES;"";</conn2>

When I run the program I get the following error at this line of code:
Code - OleDbDataAdapter adapter = new OleDbDataAdapter(QUERY, connectionString);
Error - Format of the initialization string does not conform to specification starting at index 91.

The connection string ends up as the following when loaded:
"@Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\calc;Extended Properties=;\"\"text;HDR=YES;\"\";"

Is the problem that it should look like:
"@Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\calc;Extended Properties=;""text;HDR=YES;"";"

If so how do I get the double quotes to escape correctly as a string?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top