Thursday, May 28, 2009

Adding the XML Declaration when calling DataSet.WriteXml(Stream stream) method

using (DataSet ds = new DataSet())

{

//TODO: fill the ds with appropriate data

XmlTextWriter stream = new

XmlTextWriter(@"C:/xmltest.xml", Encoding.UTF8);

ds.WriteXml(stream);

stream.Close();

}



The above example attempts to write the content of the DataSet to an xml file using the DataSet.WriteXml method. The xml file doesn't contain the XML declaration.

Content of xml file:

<NewDataSet />



TIP: To include the XML declaration in the output file, call the method WriteStartDocument() on the stream object prior to the WriteXml() method call.

using (DataSet ds = new DataSet())

{

//TODO: fill the ds with appropriate data

XmlTextWriter stream = new

XmlTextWriter(@"C:/xmltest.xml", Encoding.UTF8);

stream.WriteStartDocument();

ds.WriteXml(stream);

stream.Close();

}



Content of xml file:

<?xml version="1.0" encoding="utf-8"?>

<NewDataSet />

Sunday, May 24, 2009

Application Settings in .Net Framework

The .Net Framework allows us to create and access values that persist from application session to application session. These values are called settings.For example, settings can be used to store user preferences, color scheme of application, or web server address used by the application.

Settings have four properties: Name (also used to access it at run-time), Type (represents type of setting), Value and Scope (can be either User or Application). A setting with Application scope represents a value that is used by the entire application regardless of the user, whereas a setting with User scope is more likely to be user-specific.

Application settings are read-only and values can be changed at design time, while User settings can be changed at run-time as well.

The Settings Editor in Visual Studio can be opened by double click Settings.settings under the Properties folder in Solution Explorer.



Loading settings at run-time

this.Title = Properties.Settings.Default.ApplicationName;

txtName.Text = Properties.Settings.Default.Name;

txtAddress.Text = Properties.Settings.Default.Address;

txtNationality.Text = Properties.Settings.Default.Nationality;

Saving user setings at run-time

Properties.Settings.Default.Name = txtName.Text;

Properties.Settings.Default.Nationality = txtNationality.Text;

Properties.Settings.Default.Address = txtAddress.Text;

Properties.Settings.Default.Save();



Download Example

Thursday, May 14, 2009

Single Instance applications using WindowsFormsApplicationBase available in Microsoft.VisualBasic.dll

Have you ever thought of using the WindowsFormsApplicationBase class (available in Microsoft.VisualBasic.dll) in a C# solution to implement a Single Instance application?

Actually, there are a number of ways to write Single Instance applications, such as Mutexes, Processes and so on and so forth. However, the WindowsFormsApplicationBase class provides a very elegant way to achieve the same result.

Click here, here and here for some cool examples and as such, I don't need to write any sample code and/or demo here ;-)