【问题标题】:Visual Basic Array Data to XMLVisual Basic 数组数据到 XML
【发布时间】:2019-02-03 21:25:39
【问题描述】:

努力将数组的内容导出到 XML 文件。我在第 101 行有一个错误。任何人都可以在这里发现错误吗?还是我使用了错误的方法?

谢谢大家!!

维克

代码:

createNode((Str(i + 1)), strArray(i, 0), strArray(i, 1), strArray(i, 2), strArray(i, 3), strArray(i, 4), strArray(i, 5), writer)

错误:

错误 2 类型“System.Xml.XmlTextWriter”的值无法转换 到“字符串”。

错误 1 ​​未为“Private Sub”的参数“writer”指定参数 createNode(intQuantity As String, pNumber As String, pGiven As String, pFamily 作为字符串,pResult 作为字符串,pTotal 作为字符串,pPercent 作为 String, pGrade As String, writer As System.Xml.XmlTextWriter)'。

这是我的代码:

Imports System.Xml

Public Class frmStudentGrades
    ' Author: Victoria Farrell
    ' Date: August 2018
    'Purpose: To calculate the display the graded results of student marks entered.

    Dim intQuantity As Integer                          ' This variable handles the total number of students in the class
    Dim strArray(intQuantity, 5) As String              ' This Array handles all the data - student names, marks, percentages and final grades
    Dim Counter As Integer                              ' This variable counts how many students are entered and handles each row in the array.

    Private Sub btnSize_Click(sender As Object, e As EventArgs) Handles btnSize.Click

        ' This subroutine reads in the number of students in the class and informs the user to enter student data.
        ' Counter is set to zero at this point to intialise the first point in the array.

        intQuantity = Val(txtQuantity.Text)
        Counter = 0
        MsgBox("Your class has " & intQuantity & " students. Please enter each student and their marks one by one.")
    End Sub

    Private Sub btnStudent_Click(sender As Object, e As EventArgs) Handles btnStudent.Click

        Dim strArray(intQuantity, 5) As String
        ' The array is defined in terms of the size of the class (numbers of students)

        If Counter < intQuantity Then

            MsgBox("You have entered student number " & (Counter + 1))
            ' This message informs the user that they have entered data into the array and how many students have been entered based on the counter.


            strArray(Counter, 0) = txtGivenName.Text     ' The student given name is entered into the first column of the Counter-th row
            strArray(Counter, 1) = txtFamilyName.Text    ' The student family name is entered into the second column of the Counter-th row
            strArray(Counter, 2) = txtResult.Text        ' The student's result is entered into the third column of the Counter-th row
            strArray(Counter, 3) = txtTotal.Text         ' The total marks for the assessment is entered into the fourth column of the Counter-th row

            strArray(Counter, 4) = CStr(Math.Round(((Val(strArray(Counter, 2)) / Val(strArray(Counter, 3))) * 100), 2))

            ' This calcuates the percentage of the results and enters it into the fifth column of the Counter-th row

            ' This IF statement tests the percentage and decides what the Letter Grade will be allocated. this goes into the sixth column.
            If strArray(Counter, 4) < 50 Then
                strArray(Counter, 5) = "F"
            ElseIf strArray(Counter, 4) >= 50 And strArray(Counter, 4) < 60 Then
                strArray(Counter, 5) = "E"
            ElseIf strArray(Counter, 4) >= 60 And strArray(Counter, 4) < 70 Then
                strArray(Counter, 5) = "D"
            ElseIf strArray(Counter, 4) >= 70 And strArray(Counter, 4) < 80 Then
                strArray(Counter, 5) = "C"
            ElseIf strArray(Counter, 4) >= 80 And strArray(Counter, 4) < 90 Then
                strArray(Counter, 5) = "B"
            ElseIf strArray(Counter, 4) >= 90 And strArray(Counter, 4) <= 100 Then
                strArray(Counter, 5) = "A"
            End If


            ' The content of the entire row is added to the list box to be displayed.
            listResults.Items.Add(strArray(Counter, 0) & " " & strArray(Counter, 1) & " " & strArray(Counter, 2) & "/" & strArray(Counter, 3) & " :  " & strArray(Counter, 4) & "% Final Grade: " & strArray(Counter, 5))

            ' One is added to the counter because the row is complete and we need to count it. 
            Counter = Counter + 1

            ' when the Counter reaches the size of the class a warning will appear if they try to enter more students.
        ElseIf Counter = intQuantity Then
            MsgBox("You have entered all the students into your class")

        End If


    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

        ' This subroutine allows the user to clear the data from all the text boxes.

        txtGivenName.Text = " "
        txtFamilyName.Text = " "
        txtResult.Text = " "
        txtTotal.Text = " "
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

        ' This subroutine allows the user to close the program.

        Me.Close()

    End Sub

        Private Sub btnXml_Click(sender As Object, e As EventArgs) Handles btnXml.Click

    Dim writer As New XmlTextWriter("StudentResults.xml", System.Text.Encoding.UTF8)

    writer.WriteStartDocument(True)
    writer.Formatting = Formatting.Indented
    writer.Indentation = 2
    writer.WriteStartElement("Table")

    For i As Integer = 0 To intQuantity

        createNode((i + 1), strArray(i, 0), strArray(i, 1), strArray(i, 2), strArray(i, 3), strArray(i, 4), strArray(i, 5), writer)

    Next i

    writer.WriteEndElement()
    writer.WriteEndDocument()
    writer.Close()
End Sub

Private Sub createNode(ByVal intQuantity As String, pNumber As String, pGiven As String, pFamily As String, pResult As String, pTotal As String, pPercent As String, pGrade As String)

    Dim writer As New XmlTextWriter("StudentResults.xml", System.Text.Encoding.UTF8)

    For i As Integer = 0 To intQuantity



        writer.WriteStartElement("Student_Number")
        writer.WriteString(pNumber)
        writer.WriteEndElement()

        writer.WriteStartElement("Student_Given_Name")
        writer.WriteString(pGiven)
        writer.WriteEndElement()

        writer.WriteStartElement("Student_Family_Name")
        writer.WriteString(pFamily)
        writer.WriteEndElement()

        writer.WriteStartElement("Student_Result")
        writer.WriteString(pResult)
        writer.WriteEndElement()

        writer.WriteStartElement("Student_Total")
        writer.WriteString(pTotal)
        writer.WriteEndElement()

        writer.WriteStartElement("Student_Percentage")
        writer.WriteString(pPercent)
        writer.WriteEndElement()

        writer.WriteStartElement("Student_Grade")
        writer.WriteString(pGrade)
        writer.WriteEndElement()


    Next

End Sub

结束类

【问题讨论】:

    标签: arrays xml vb.net


    【解决方案1】:

    您使用 8 个值调用您的 createNode 函数。您在调用中的第 8 个值是 writer,这是一个 XmlTextWriter,但在您的 createNode 函数中,第 8 个值是 pGrade As String。当您调用 createNode 函数时,writer 必须是第 9 个值。此外,当您的函数要求 9 个值时,您需要提供 9 个值。

    您应该再次检查您的代码。当线路变得如此长时,它可能会非常混乱。也不需要ByVal。您可以将其从您的 createNode 函数中删除。

    【讨论】:

    • 谢谢马可。我对我的问题进行了一些编辑,但我仍然收到一个错误:'System.Xml.XmlTextWriter' 类型的错误 1 ​​值无法转换为'String'。 '如果这看起来像是一个新手错误,我很抱歉,但我只是一名学校老师。再次感谢。
    • @Fazz1967withExplosions 是的,因为您从 createNode Sub 中删除了 XMLWriter,而 writer 仍位于第 8 位(这是一个名为 pGrade 的字符串)。再次将 XMLWriter 添加到您的 Sub 并检查您是否真的需要 createNode Sub 中的所有值。小提示:写在 Sub 要求的调用之上。然后就可以直接看到问题了。这里是一个例子:screen.png - 我希望你现在明白这个问题。
    • 重新安装VS后问题解决。其他几个安装也遇到了同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2021-10-03
    • 2011-11-18
    相关资源
    最近更新 更多