XmlNode.InnerText 如果数据为空则中断

XmlNode.InnerText breaks if data is null

我在 VS 2008 中有一个脚本任务。在这个脚本任务中,我得到一个数据集并将其转换为 XML,并循环遍历节点:

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

我遇到的问题是 InsertAssessmentHistory 方法,特别是调用 CurrentAssessment.Item("SCORE_RESULTS").InnerText。数据集为其中一条记录 returning 空数据,看起来当对该记录调用 .InnerText 时失败并显示 "Object Reference not set to an instance of an object" 因为 XML 标记“... ".

有没有办法调用.InnerText,如果没有数据那么return null?

在@codexer 的指导下,我能够让它工作。内部循环现在看起来像这样:

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

为什么要转换为 Xml,而您可以使用数据集实现此目的。 这是您作为答案提供的内部循环的数据集版本:

For Each CurrentAssessment As DataRow In dsCurrentAssessment.Tables(0).Rows

    Dim ScoreResults As String
    Dim Interpretation As String


    If CurrentAssessment.Item("SCORE_RESULTS") Is Nothing OrElse _
        String.IsNullOrEmpty(CurrentAssessment.Item("SCORE_RESULTS").ToString) Then
        ScoreResults = Nothing
    Else
        ScoreResults = CurrentAssessment.Item("SCORE_RESULTS").ToString
    End If


    If CurrentAssessment.Item("INTERPRETATION") Is Nothing OrElse _
        String.IsNullOrEmpty(CurrentAssessment.Item("INTERPRETATION").ToString) Then
        Interpretation = Nothing
    Else
        Interpretation = CurrentAssessment.Item("INTERPRETATION").ToString
    End If


    InsertAssessmentHistory(HistInstanceID, _
        CurrentAssessment.Item("TITLE").ToString, _
        CurrentAssessment.Item("CONTENT").ToString, _
        CurrentAssessment.Item("TYPE").ToString, _
        CDate(convertAssessmentDate(CurrentAssessment.Item("ADMINISTRATION_DATE").ToString)), _
        CurrentAssessment.Item("REPORTING_METHOD").ToString, _
        ScoreResults, _
        Interpretation, _
        CStr(htHistory("ASSESSMENT_ACTION")))
Next