运行VB 2010 XML转换代码的错误

时间:2011-03-06 06:24:57

标签: .net vb.net visual-studio-2010

我有这段代码:

Public Class Transform
    Dim inputFile As IO.StringReader ' Object variable
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdOpenFile.FileOk
        ' Configure the Open dialog box and display it. 
        With ofdOpenFile
            .Filter = " Text files (*. txt)|*. txt| All files (*.*)|*.*"
            .InitialDirectory = " C:\ Data"
            .Title = " Select a File to Open"
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                inputFile = IO.File.OpenText(.FileName)
            End If
        End With
    End Sub

    Private Sub Transform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Option Strict On
Imports System.Xml.Xsl
        ''' <summary>
        ''' Updated 10.14.2010 - changed class name to Form1
        ''' This program converts a VB 2008 XML Documentation 
        ''' into an HTML file for displaying in a browser using
        ''' an XSLT transformation file stored on a remote server
        ''' </summary>
        ''' <author>John Couture - jcouture@sdccd.edu</author>
        ''' <assignment>Week 5</assignment>
        ''' <version>2.02 - 02/03/2010</version>
        ''' <seealso>http://media.techtarget.com/digitalguide/images/Misc/professionalVB_ch12.pdf </seealso>
        ''' <seealso>Using XML in Visual Basic 2005, 
        ''' Excerpted from Wrox Publishing: Professional VB 2005, 
        ''' (c)2005, Bill Evjen et al, ISBN 0-764507536-8 
        ''' </seealso> 
        ''' 
    Public Class Form1
        ' Identify where the XSLT file is located
        Dim strTransformPath As String = Application.StartupPath & "\transform.xslt"

        ' Provide a default file name for this program
        Dim strOutputPath As String = _
            Application.StartupPath & "/output.htm"

        ''' <summary>
        ''' When the program first starts, show the user where the output
        ''' file will be located.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
    End Sub

    Private Sub Transform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtHTML.Text = strOutputPath
    End Sub

    ''' <summary>
    ''' Using a file dialog box, get the input file.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnXML_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnXML.Click
        With ofdOpenFile
            .InitialDirectory = "C:\"
            .Filter() = "Comment File | *.xml"
            .ShowDialog()
            txtXML.Text = .FileName
        End With
    End Sub
    ''' <summary>
    ''' Transform the XML code into HTML code
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>
    ''' Once you know the location of the XML file, the computer
    ''' will automatically download the XSLT (transformation) file
    ''' and convert the XML code into HTML code for printing.
    ''' </remarks>
    Private Sub btnTransform_Click(ByVal sender As System.Object, _
           ByVal e As System.EventArgs) _
           Handles btnTransform.Click

        Dim myXSLTransform As XslCompiledTransform = New XslCompiledTransform
        Try
            'myXSLTransform.Load(txtXSLT.Text)
            myXSLTransform.Load(strTransformPath)

            myXSLTransform.Transform(txtXML.Text, txtHTML.Text)
            btnTransform.Text = "Transformation is Done!"
            btnTransform.BackColor = System.Drawing.Color.Green
            btnTransform.ForeColor = System.Drawing.Color.White
            btnTransform.Font = New Font(btnTransform.Name, 12, FontStyle.Bold)

        Catch ex As Exception
            If Not IO.File.Exists(strTransformPath) Then
                MessageBox.Show("Cannot find XSL file")
            End If
            If Not IO.File.Exists(txtXML.Text) Then
                MessageBox.Show("Cannot find XML file")
            End If
            MessageBox.Show("Message: " & ex.Message)
        End Try
    End Sub
End Class

我收到了这些错误:

  

'Private Sub Transform_Load(sender As Object,e As System.EventArgs)'具有多个具有相同签名的定义。第15行

     

'strOutputPath'未声明。由于其保护级别,它可能无法访问。第51行

     

未定义类型'XslCompiledTransform'。第83行

Public Class Transform
    Dim inputFile As IO.StringReader ' Object variable
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdOpenFile.FileOk
        ' Configure the Open dialog box and display it. 
        With ofdOpenFile
            .Filter = " Text files (*. txt)|*. txt| All files (*.*)|*.*"
            .InitialDirectory = " C:\ Data"
            .Title = " Select a File to Open"
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                inputFile = IO.File.OpenText(.FileName)
            End If
        End With
    End Sub

    Private Sub Transform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Option Strict On
Imports System.Xml.Xsl
        ''' <summary>
        ''' Updated 10.14.2010 - changed class name to Form1
        ''' This program converts a VB 2008 XML Documentation 
        ''' into an HTML file for displaying in a browser using
        ''' an XSLT transformation file stored on a remote server
        ''' </summary>
        ''' <author>John Couture - jcouture@sdccd.edu</author>
        ''' <assignment>Week 5</assignment>
        ''' <version>2.02 - 02/03/2010</version>
        ''' <seealso>http://media.techtarget.com/digitalguide/images/Misc/professionalVB_ch12.pdf </seealso>
        ''' <seealso>Using XML in Visual Basic 2005, 
        ''' Excerpted from Wrox Publishing: Professional VB 2005, 
        ''' (c)2005, Bill Evjen et al, ISBN 0-764507536-8 
        ''' </seealso> 
        ''' 
    Public Class Form1
        ' Identify where the XSLT file is located
        Dim strTransformPath As String = Application.StartupPath & "\transform.xslt"

        ' Provide a default file name for this program
        Dim strOutputPath As String = _
            Application.StartupPath & "/output.htm"

        ''' <summary>
        ''' When the program first starts, show the user where the output
        ''' file will be located.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
    End Sub

    Private Sub Transform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtHTML.Text = strOutputPath
    End Sub

    ''' <summary>
    ''' Using a file dialog box, get the input file.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnXML_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnXML.Click
        With ofdOpenFile
            .InitialDirectory = "C:\"
            .Filter() = "Comment File | *.xml"
            .ShowDialog()
            txtXML.Text = .FileName
        End With
    End Sub
    ''' <summary>
    ''' Transform the XML code into HTML code
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>
    ''' Once you know the location of the XML file, the computer
    ''' will automatically download the XSLT (transformation) file
    ''' and convert the XML code into HTML code for printing.
    ''' </remarks>
    Private Sub btnTransform_Click(ByVal sender As System.Object, _
           ByVal e As System.EventArgs) _
           Handles btnTransform.Click

        Dim myXSLTransform As XslCompiledTransform = New XslCompiledTransform
        Try
            'myXSLTransform.Load(txtXSLT.Text)
            myXSLTransform.Load(strTransformPath)

            myXSLTransform.Transform(txtXML.Text, txtHTML.Text)
            btnTransform.Text = "Transformation is Done!"
            btnTransform.BackColor = System.Drawing.Color.Green
            btnTransform.ForeColor = System.Drawing.Color.White
            btnTransform.Font = New Font(btnTransform.Name, 12, FontStyle.Bold)

        Catch ex As Exception
            If Not IO.File.Exists(strTransformPath) Then
                MessageBox.Show("Cannot find XSL file")
            End If
            If Not IO.File.Exists(txtXML.Text) Then
                MessageBox.Show("Cannot find XML file")
            End If
            MessageBox.Show("Message: " & ex.Message)
        End Try
    End Sub
End Class

我收到了这些错误:

  

'Private Sub Transform_Load(sender As Object,e As System.EventArgs)'具有多个具有相同签名的定义。第15行

     

'strOutputPath'未声明。由于其保护级别,它可能无法访问。第51行

     

未定义类型'XslCompiledTransform'。第83行

1 个答案:

答案 0 :(得分:0)

该代码存在严重问题。您有许多方法(子和函数)不会以相应的End SubEnd Function行终止。确保您已从原始来源以正确的顺序将完全复制

您获得的编译器错误看起来非常直观且易于理解,前提是您需要时间仔细考虑每个错误:

  1. 第一个意味着方法Transform_Load在几个不同的地方以完全相同的方式被声明。这是不允许的,因为编译器不知道应该使用哪一个。简单的解决方法是删除重复的定义(尽管如前所述,在您的情况下,原因似乎是您的代码搞砸了。)

  2. 第二个意味着当您尝试使用它时,未声明名为strOutputPath的变量。声明是告诉编译器变量的方式。您不能使用变量(通过将值分配给,或者从中读取值),直到您告诉编译器有关它。确保您在一个范围内声明了一个名为strOutputPath的变量,该范围可供您尝试访问它的代码使用。 (通常,这将与您在其中使用的方法体相同。)

  3. 第三个错误表示永远不会定义类型XslCompiledTransform。当您使用此行声明其实例时,您尝试使用该类型,如类或结构:

    Dim myXSLTransform As XslCompiledTransform = New XslCompiledTransform
    

    但编译器之前从未听说过这种类型,因此它不允许您声明它的实例。确保您复制了原始代码的所有,包括XslCompiledTransform类型的声明。