I have a script task in VS 2008. In this script task I get a Data Set and convert it into XML, and loop through the nodes:
Dim dsHistory As New DataSet
dsHistory = GetHistoryByYear(Year)
Dim HistoryDoc As New XmlDocument
HistoryDoc.LoadXml(dsHistory.GetXml)
Dim HistoryStudentList As XmlNodeList
Dim StudentNode as XmlNode
HistoryStudentList = HistoryDox.SelectNodes("/NewDataSet/Table")
dsHistory = Nothing
For Each HistoryStudentNode In HistoryStudentList
Dim dsCurrentAssessment As New DataSet
dsCurrentAssessment = GetCurrentAssessmentData(...)
...code to make sure dataset has data...
Dim CurrentAssessmentDoc As New XmlDocument
CurrentAssessmentDoc.LoadXml(dsCurrentAssessment.GetXml)
Dim CurrentAssessmentNode As XmlNode
CurrentAssessmentNode = CurrentAssessmentDoc.SelectSingleNode("/NewDataSet")
For Each CurrentAssessment As XmlNode In CurrentAssessmentNode.SelectNodes("Table")
InsertAssessmentHistory(HistInstanceID, CurrentAssessment.Item("TITLE").InnerText, CurrentAssessment.Item("CONTENT").InnerText, CurrentAssessment.Item("TYPE").InnerText, CDate(convertAssessmentDate(CurrentAssessment.Item("ADMINISTRATION_DATE").InnerText)), CurrentAssessment.Item("REPORTING_METHOD").InnerText, CurrentAssessment.Item("SCORE_RESULTS").InnerText, CurrentAssessment.Item("INTERPRETATION").InnerText, CStr(htHistory("ASSESSMENT_ACTION")))
Next
Next
The issue I am having is with the InsertAssessmentHistory method, specifically calling CurrentAssessment.Item("SCORE_RESULTS").InnerText. The data set is returning null data for one of the records, and it looks like when .InnerText is called on that record it fails with "Object Reference not set to an instance of an object" because the XML tag "...".
Is there a way to call .InnerText, and if there's no data then return null?
With @codexer's guidance I was able to get it to work. The inner loop now looks like this:
For Each CurrentAssessment As XmlNode In CurrentAssessmentNode.SelectNodes("Table")
Dim ScoreResults As String
Dim Interpretation As String
If CurrentAssessment.Item("SCORE_RESULTS") Is Nothing Then
ScoreResults = Nothing
Else
If CurrentAssessment.Item("SCORE_RESULTS").InnerText Is Nothing Then
ScoreResults = Nothing
Else
ScoreResults = CurrentAssessment.Item("SCORE_RESULTS").InnerText
End If
End If
If CurrentAssessment.Item("INTERPRETATION") Is Nothing Then
Interpretation = Nothing
Else
If CurrentAssessment.Item("INTERPRETATION").InnerText Is Nothing Then
Interpretation = Nothing
Else
Interpretation = CurrentAssessment.Item("INTERPRETATION").InnerText
End If
End If
InsertAssessmentHistory(HistInstanceID, _
CurrentAssessment.Item("TITLE").InnerText, _
CurrentAssessment.Item("CONTENT").InnerText, _
CurrentAssessment.Item("TYPE").InnerText, _
CDate(convertAssessmentDate(CurrentAssessment.Item("ADMINISTRATION_DATE").InnerText)), _
CurrentAssessment.Item("REPORTING_METHOD").InnerText, _
ScoreResults, _
Interpretation, _
CStr(htHistory("ASSESSMENT_ACTION")))
Next