I'm having the darndest XML config file problem that I really need help with.
I'm supporting a .NET 1.1 desktop application with its own config file, and
I implement IConfigurationSectionHandler so I can have a custom config
section in my config file. The IConfigurationSectionHandler.Create method
just returns the 'section' XmlNode back to the consumer. The config file
looks kind of like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MyConfigSettings" type="MyAssembly.ConfigReader, MyAssembly" />
</configSections>
<appSettings>
</appSettings>
<MyConfigSettings>
<ConfigSet zone="Test">
<add key="FirstKey" value="FirstValue"/>
<add key="SecondKey" value="SecondValue"/>
<add key="ThirdKey" value="ThirdValue"/>
</ConfigSet>
<ConfigSet zone="Integration">
<add key="FirstKey" value="FirstValue"/>
<add key="SecondKey" value="SecondValue"/>
<add key="ThirdKey" value="ThirdValue"/>
</ConfigSet>
</MyConfigSettings>
</configuration>
Then I've got this section of code that looks like this:
Public Shared Sub SetConfigZone(ByVal Zone As String)
Dim debug As String
Try
debug &= "Checking Zone variable " & Environment.NewLine
If (Zone Is Nothing) Then
MessageBox.Show("Zone variable passed in is nothing")
End If
debug &= "Getting XmlNode from config file " & Environment.NewLine
Dim parent As XmlNode =
ConfigurationSettings.GetConfig("MyConfigSettings" )
debug &= "Checking parent variable " & Environment.NewLine
If (parent Is Nothing) Then
Throw New ConfigurationException("Unable to locate the
custom configuration section MyConfigSettings. Please check your config
file.")
End If
MessageBox.Show(parent.OuterXml)
debug &= "Beginning loop through nodes " & Environment.NewLine
For Each node As XmlNode In parent.ChildNodes
MessageBox.Show(node.OuterXml)
debug &= "Inspecting Node's zone attribute... " &
Environment.NewLine
If (node.Attributes("zone").Value = Zone) Then
debug &= "Correct node found... " & Environment.NewLine
debug &= "Creating new NameValueCollection... " &
Environment.NewLine
ConfigCommon.m_ConfigSettings = New NameValueCollection
debug &= "Looping through 'add' subnodes... " &
Environment.NewLine
For Each subNode As XmlNode In node.SelectNodes("add")
debug &= "Getting subnode's 'key' value... " &
Environment.NewLine
ConfigCommon.m_ConfigSettings.Add(subNode.Attribut es("key").Value,
subNode.Attributes("value").Value)
Next
Return
End If
Next
Catch ex As Exception
MessageBox.Show(debug, "Our debug message")
MessageBox.Show(ex.ToString(), "Our error message")
End Try
'If we get this far, we haven't found our configuration section
Throw New ConfigurationException("Unable to locate configuration
zone " & Zone)
End Sub
So here's my problem. This code has been working just great for the last
six to nine months, but all of a sudden about two weeks ago all of the new
machine builds our desktop group puts out fail at the following line:
If (node.Attributes("zone").Value = Zone) //fails on first
run through
On new machines, it's producing a NullReferenceException. But when I take
the exact same code and run it on anyone else's machine, it works just fine.
In fact, you'll notice that I added a lot of little debugs and
MessageBox.Show statements in there to try to pinpoint it. When I run the
code on my machine, I notice that the MessageBox.Show(node.OuterXml) produces
the first node. When I run this on a newly built machine, the
MessageBox.Show produces an empty string.
My question is: what is missing from these new builds that was in the old
builds? I think I've ruled out different versions of the .NET framework.
Even if this was a breaking change in .NET 2.0, I tried forcing the .NET
Framework in the config file to 1.1 and still got the same error. And the
users definitely have the .NET 1.1 Framework.
Please help.
Thanks.