Thus if the config file is like:
Then the class to read it could look like:-Code:<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <!-- Extra custom configuration sections --> <section name="monitoredPrinters" type="PrinterMonitorService.CustomConfigurationSectionReaders.MonitoredPrintersSectionReader, PrinterMonitorService" /> </configSections> <monitoredPrinters> <!-- DeviceName = The unique name by which the printer is known --> <monitoredPrinter DeviceName="HP LaserJet 5L"/> <monitoredPrinter DeviceName="Microsoft Office Document Image Writer"/> </monitoredPrinters> </configuration>
VB Code:
Public NotInheritable Class MonitoredPrintersSectionReader Implements IConfigurationSectionHandler #Region "Private constants" Private Const SECTION_NAME As String = "monitoredPrinters" Private Const ATTRIBUTE_NAME As String = "monitoredPrinter" #End Region Shared Sub New() ConfigurationSettings.GetConfig(SECTION_NAME) End Sub Public Shared Function GetMonitoredPrintersCollection() As Collection Return CType(System.Configuration.ConfigurationSettings.GetConfig(SECTION_NAME), Collection) End Function #Region "IConfigurationSectionHandler interface" Public Function Create( _ ByVal parent As Object, _ ByVal configContext As Object, _ ByVal section As XmlNode _ ) As Object Implements IConfigurationSectionHandler.Create Dim retVal As New Collection() If section.HasChildNodes Then Dim childNode As XmlNode For Each childNode In section.ChildNodes If childNode.Name = ATTRIBUTE_NAME Then retVal.Add(childNode.Attributes.ItemOf("DeviceName").Value) End If Next End If Return retVal End Function #End Region Private Sub New() '\\ make this class non creatable End Sub End Class
Hope this is useful,
Duncan




Reply With Quote