Parse XML

  • Thread starter Thread starter Martin
  • Start date Start date
M

Martin

I'm looking for a VB snippet to return the names of the
sections in the XML below, can anyone help?


<configuration>
<configSections>
<sectionGroup name="GroupName">
<section name="SectionName1"/>
<section name="SectionName2"/>
</sectionGroup>
</configSections>
</configuration>
 
Martin,
See the following on how to create new sections via the configSections
section.

http://msdn.microsoft.com/library/d...de/html/cpconconfigurationsectionhandlers.asp

and:
http://msdn.microsoft.com/library/d...ref/html/gngrfconfigurationsectionsschema.asp


Note rather then use SingleTagSectionHandler & NameValueSectionHandler, I
normally inherit from System.Configuration.DictionarySectionHandler &
override the KeyAttributeName & ValueAttributeName to give more meaningful
names in the app.config,

Something like (showing SingleTagSectionHandler, NameValueSectionHandler &
DictionarySectionHandler):

<configuration>
<configSections>
<sectionGroup name="GroupName">
<section name="SectionName1"
type="System.Configuration.SingleTagSectionHandler" />
<section name="SectionName2"
type="System.Configuration.NameValueSectionHandler" />
<section name="SectionName3" type="MyProject.CustomSectionHandler,
MyProject" />
</sectionGroup>
</configSections>
<GroupName>
<SectionName1 key1="value1" key2="value2" key3="value3" />
<SectionName2>
<add key="key1" value="value1" />
<add key="key2" value="value2" />
<add key="key3" value="value3" />
</SectionName2>
<SectionName3>
<add form="form1" type="MyProject.Form1, MyProject" />
<add form="form2" type="MyProject.Form2, MyProject" />
</SectionName3>
</GroupName>
</configuration>

Public Class CustomSectionHandler
Inherits DictionarySectionHandler

Protected Overrides ReadOnly Property KeyAttributeName() As String
Get
Return "form"
End Get
End Property

Protected Overrides ReadOnly Property ValueAttributeName() As String
Get
Return "type"
End Get
End Property
End Class

Public Sub Main()
Dim sectionName1 As IDictionary =
DirectCast(ConfigurationSettings.GetConfig("GroupName/SectionName1"),
IDictionary)
Dim sectionName2 As NameValueCollection =
DirectCast(ConfigurationSettings.GetConfig("GroupName/SectionName2"),
NameValueCollection)
Dim sectionName3 As IDictionary =
DirectCast(ConfigurationSettings.GetConfig("GroupName/SectionName3"),
IDictionary)

Dim value1_1 As String = DirectCast(sectionName1("key1"), String)
Dim value1_2 As String = DirectCast(sectionName1("key2"), String)
Dim value1_3 As String = DirectCast(sectionName1("key3"), String)

Dim value2_1 As String = DirectCast(sectionName2("key1"), String)
Dim value2_2 As String = DirectCast(sectionName2("key2"), String)
Dim value2_3 As String = DirectCast(sectionName2("key3"), String)

Dim type1 As String = DirectCast(sectionName3("form1"), String)
Dim type2 As String = DirectCast(sectionName3("form2"), String)


End Sub

Of course you can implement IConfigurationSectionHandler if the above is not
enough...

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top