views:

38

answers:

1

I'm having a problem with looping through an XML file and storing the value in a singleton

My XML looks like this

<values>
    <value></value>
    <value>$1</value>           
    <value>$5,000</value>       
    <value>$10,000</value>      
    <value>$15,000</value>      
    <value>$25,000</value>      
    <value>$50,000</value>      
    <value>$75,000</value>      
    <value>$100,000</value>     
    <value>$250,000</value>     
    <value>$500,000</value>     
    <value>$750,000</value>     
    <value>$1,000,000</value>   
    <value>$1,250,000</value>   
    <value>$1,500,000</value>   
    <value>$1,750,000</value>   
    <value>$2,000,000</value>   
    <value>$2,500,000</value>   
    <value>$3,000,000</value>   
    <value>$4,000,000</value>   
    <value>$5,000,000</value>   
    <value>$7,500,000</value>   
    <value>$10,000,000</value>  
    <value>$15,000,000</value>  
    <value>$25,000,000</value>  
    <value>$50,000,000</value>  
    <value>$100,000,000</value> 
    <value>$100,000,000+</value>
</values>

And my function looks like this

Public Class LoadValues
    Private Shared SearchValuesInstance As List(Of SearchValues) = Nothing

    Public Shared ReadOnly Property LoadSearchValues As List(Of SearchValues)
        Get
            Dim sv As New List(Of SearchValues)

            If SearchValuesInstance Is Nothing Then
                Dim objDoc As XmlDocument = New XmlDataDocument
                Dim objRdr As XmlTextReader = New XmlTextReader(HttpContext.Current.Server.MapPath("~/App_Data/Search-Values.xml"))
                objRdr.Read()
                objDoc.Load(objRdr)

                Dim root As XmlElement = objDoc.DocumentElement
                Dim itemNodes As XmlNodeList = root.SelectNodes("/values")

                For Each n As XmlNode In itemNodes
                    sv.Add(New SearchValues(n("@value").InnerText, n("@value").InnerText))
                Next
                SearchValuesInstance = sv

            Else : sv = SearchValuesInstance
            End If

            Return sv
        End Get
    End Property

End Class

My problem is that I'm getting an object not set to an instance of an object on the sv.Add(New SearchValues(n("@value").InnerText, n("@value").InnerText)) line.

A: 

Ok, I seem to have found my answer. Hopefully this will help the next person with the same problem.

Public Class LoadValues
    Private Shared SearchValuesInstance As List(Of SearchValues) = Nothing

    Public Shared ReadOnly Property LoadSearchValues As List(Of SearchValues)
        Get
            Dim sv As New List(Of SearchValues)

            If SearchValuesInstance Is Nothing Then

                Dim objDoc As XmlDocument = New XmlDataDocument
                objDoc.Load(HttpContext.Current.Server.MapPath("~/App_Data/Search-Values.xml"))

                Dim root As XmlNode = objDoc.DocumentElement
                Dim nodelist As XmlNodeList = root.SelectNodes("/values/value")

                Dim value As Double

                For Each n As XmlNode In nodelist
                    value = If(BizHelper.ConvertToDouble(n.InnerText) Is Nothing, 0, BizHelper.ConvertToDouble(n.InnerText))
                    sv.Add(New SearchValues(n.InnerText, value))
                Next

                SearchValuesInstance = sv

            Else : sv = SearchValuesInstance
            End If

            Return sv
        End Get
    End Property

    Public Shared Sub DisposeSearchValues()
        SearchValuesInstance = Nothing
    End Sub

End Class
rockinthesixstring