2010-05-01 121 views
0

我在通過一個XML文件循環,並在單VB.NET通過XML循環存儲在單

我的XML看起來像這樣

<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> 

存儲值存在問題而我的功能看起來像這樣

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 

我的問題是,我在sv.Add(New SearchValues(n("@value").InnerText, n("@value").InnerText))線得到一個object not set to an instance of an object

回答

0

好吧,我似乎找到了我的答案。希望這會幫助有同樣問題的下一個人。

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