【问题标题】:Errors running VB 2010 XML Transform code运行 VB 2010 XML 转换代码时出错
【发布时间】:2011-03-06 06:24:57
【问题描述】:

我有这个代码:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    1. 第一个表示方法Transform_Load 在几个不同的地方以完全相同的方式声明。这是不允许的,因为编译器不知道它应该使用哪一个。简单的解决方法是删除重复的定义(尽管,就像我之前说的,在你的情况下,原因似乎是你的代码搞砸了)。

    2. 第二个意思是strOutputPath这个变量在你尝试使用的时候没有声明。声明是您告诉编译器有关变量的方式。在您告诉编译器之前,您不能使用变量(通过为它分配一个值,或者从它读取一个值)。确保您已在您尝试访问它的代码可用的范围内声明了一个名为 strOutputPath 的变量。 (通常,这将在您使用它的同一方法体中。)

    3. 第三个错误表明类型XslCompiledTransform 从未定义。当您使用以下行声明它的实例时,您正在尝试像 Class 或 Structure 一样使用该类型:

      Dim myXSLTransform As XslCompiledTransform = New XslCompiledTransform
      

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 2021-08-04
      • 2014-04-26
      相关资源
      最近更新 更多