Sunday, October 09, 2005

Using XML as a source for Application Configuration ettings.

A question at CNUG
spurred me to find the possible solutions .You have all the right reasons to choose XML for application configuration settings.Storing this information in an XML file will not only increase speed,but also with the data in XML, implementing the code in new applications is a breeze.For this you can apply the following options:

1)Configuration Setting using ConfigurationSettgins.AppSettings object

string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];

2)You can also implement a wwAppConfiguration subclass with custom config settings.The wwAppConfiguration class which works in conjunction with the .NET ConfigurationSettings class provides the following improvements:

* A typed interface to configuration settings
* Default values that also get written to the config file
* Read and Write access to keys
* Error handling for invalid entries
* Encryption for specific keys
* Ability to use non .config files for storage

More at this "excellent read" here

3)Also check out how to store the state and province information in an XML file, and to create a StateManager module from which you could easily extract the state information.

4) A sample of using the StateManager and programmatically accessing the State Information is available here

As for the XML files,there are more ways than one to read XML data elements using .NET .

1) Use the XMLTextReader to extract the element names and text strings from an XML file as follows :

Private Sub ReadXMLFromFile()
Dim reader As New System.Xml.XmlTextReader("c:\XMLInput.xml")
Dim contents As String = ""
While reader.Read()
If reader.NodeType = Xml.XmlNodeType.Element Then
contents &= reader.Name & ": "
End If
If reader.NodeType = Xml.XmlNodeType.Text Then
contents &= reader.Value & _
End If
End While
End Sub

NOTE : The following conditions may cause an exception:
-file exists and is read-only
-disk is full
-The path is too long
-file does not contain well-formed XML

2)Reading XML with the XmlReader
The XmlReader class enables you to:
* Verify that the characters are legal XML characters, and that element and attribute names are valid XML names.
* Verify that the XML document is well formed.
* Validate the data against a DTD or schema.
* Retrieve data from the XML stream or skip unwanted records using a pull model.

More here (although this is ocated at the winffx repositary,you shudnt have problems using the same concepts)

3) PS : the XmlValidatingReader class is obsolete in the Microsoft .NET Framework version 2.0. so i suggest not to prefer concentrating on this option .

4) Reading XML Data into a Dataset
ADO.NET provides simple methods for working with XML data that allow you to load XML data into a dataset. The dataset will then be displayed in a DataGrid control. Finally, an XML Schema based on the contents of the XML file can be displayed in a text box.

More here

5)The following link would be useful as well:

Keep Clicking,
Bhasker V K ,
Microsoft Student Champ,SVCE

1 comment:

Anonymous said...
This comment has been removed by a blog administrator.