如何从 XslTransform 迁移到 XslCompiledTransform

发布于 2024-08-25 10:47:34 字数 3732 浏览 5 评论 0原文

我有以下代码需要从 VS 2003 (.Net 1.1) 迁移到 VS 2008 (.Net 2+) 但我收到编译错误 -

System.Xml.Xsl.XslTransform' 已过时:此类已被弃用。 我可能需要改用 System.Xml.Xsl.XslCompiledTransform 。 但我没有找到可以与原始代码的所有参数一起使用的匹配的 Load() 和 Transform() 重载版本。

在 MSDN [How to: Migrate Your XslTransform Code] 中我只发现了一些更简单的情况。 http://msdn.microsoft.com/en-us /library/aa983475%28VS.80%29.aspx 但在我的代码中我看到一些注释暗示添加的参数是用来避免异常的,所以我更喜欢使用这些参数。

有人可以帮忙迁移这段代码吗?

谢谢, Atara

' ----------------------------------------------------------------------   
' VS 2003 code:
' ----------------------------------------------------------------------

. . .

Dim myXslDoc As Xml.XmlDocument

' ----------------------------------------------------------------------   
  Public Sub mcSetParameters(ByVal srcFileName As String)
' ----------------------------------------------------------------------

 Me.myXslDoc = New Xml.XmlDocument 
 Me.myXslDoc.Load(srcFileName) 

End Sub


' ----------------------------------------------------------------------    
  Public Sub mcSetHtml()
' ----------------------------------------------------------------------

 Dim oXPathNav As System.Xml.XPath.XPathNavigator = xmlDoc.DocumentElement.CreateNavigator()

 Dim sbContent As New System.Text.StringBuilder
 Dim swContent As New System.IO.StringWriter(sbContent)        

 Dim args As New System.Xml.Xsl.XsltArgumentList
 args.AddParam("paramName1", "", paramVal1.ToString)
 args.AddParam("paramName2", "", paramVal2.ToString)

 Try
   ' Try to avoid "Invalid site" exception, by using XmlUrlResolver and Evidence.
   ' If the XSLT stylesheet . . . comes from a code base that you trust, Then use Me.GetType().Assembly.Evidence() 
   Dim resolver As System.Xml.XmlUrlResolver = New System.Xml.XmlUrlResolver
   resolver.Credentials = System.Net.CredentialCache.DefaultCredentials
   Dim xslt As System.Xml.Xsl.XslTransform = New System.Xml.Xsl.XslTransform 
   xslt.Load(Me.myXslDoc, resolver, Me.GetType().Assembly.Evidence())        
   xslt.Transform(oXPathNav, args, swContent, Nothing)                       
 Catch ex As Exception
   Debug.WriteLine("Exception: {0}", ex.ToString())
 End Try

 DoSomething(sbContent.ToString())

End Sub

' ----------------------------------------------------------------------

在我的旧代码中我有 xmlDoc 和 xslDoc,所以我现在的编译代码(尚未测试)是:

' ---------------------- -----------------------------------------------------------
' VS 2008 试用代码:

' ------------------------------------------------------ ----------------------------

  Dim sbContent As System.Text.StringBuilder = New System.Text.StringBuilder()
  Dim swContent As System.IO.StringWriter = New System.IO.StringWriter(sbContent)
  Dim contentWriter As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(swContent)

  Dim args As New System.Xml.Xsl.XsltArgumentList()
  args.AddParam("paramName1", "", paramVal1.ToString)
  args.AddParam("paramName2", "", paramVal2.ToString)

  Try
    Dim resolver As New System.Xml.XmlUrlResolver
    resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

    Dim evidence As System.Security.Policy.Evidence = Me.GetType().Assembly.Evidence
    Dim secureResolver As New System.Xml.XmlSecureResolver(resolver, evidence)
    Dim settings As New System.Xml.Xsl.XsltSettings(False, True)

    Dim xslt As New System.Xml.Xsl.XslCompiledTransform()  
    xslt.Load(Me.myXslDoc, settings, secureResolver)    
    xslt.Transform(xmlDoc, args, contentWriter)  
  Catch ex As Exception
    Debug.WriteLine("Exception: {0}", ex.ToString())
  End Try

  DoSomething(sbContent.ToString())

I have the following code that I need to migrate from VS 2003 (.Net 1.1) to VS 2008 (.Net 2+)
but I get compilation error -

System.Xml.Xsl.XslTransform' is obsolete: This class has been deprecated.
I probably need to use System.Xml.Xsl.XslCompiledTransform instead.
but I do not find the matching Load() and Transform() overload versions that I can use with all the parameters of my original code.

in MSDN [How to: Migrate Your XslTransform Code] I only found some simpler cases.
http://msdn.microsoft.com/en-us/library/aa983475%28VS.80%29.aspx
but in my code I see some remarks that hints that the added parameters were used to avoid exceptions, so I prefer to use these parameters.

Can someone please help migrating this code?

Thanks,
Atara

' ----------------------------------------------------------------------   
' VS 2003 code:
' ----------------------------------------------------------------------

. . .

Dim myXslDoc As Xml.XmlDocument

' ----------------------------------------------------------------------   
  Public Sub mcSetParameters(ByVal srcFileName As String)
' ----------------------------------------------------------------------

 Me.myXslDoc = New Xml.XmlDocument 
 Me.myXslDoc.Load(srcFileName) 

End Sub


' ----------------------------------------------------------------------    
  Public Sub mcSetHtml()
' ----------------------------------------------------------------------

 Dim oXPathNav As System.Xml.XPath.XPathNavigator = xmlDoc.DocumentElement.CreateNavigator()

 Dim sbContent As New System.Text.StringBuilder
 Dim swContent As New System.IO.StringWriter(sbContent)        

 Dim args As New System.Xml.Xsl.XsltArgumentList
 args.AddParam("paramName1", "", paramVal1.ToString)
 args.AddParam("paramName2", "", paramVal2.ToString)

 Try
   ' Try to avoid "Invalid site" exception, by using XmlUrlResolver and Evidence.
   ' If the XSLT stylesheet . . . comes from a code base that you trust, Then use Me.GetType().Assembly.Evidence() 
   Dim resolver As System.Xml.XmlUrlResolver = New System.Xml.XmlUrlResolver
   resolver.Credentials = System.Net.CredentialCache.DefaultCredentials
   Dim xslt As System.Xml.Xsl.XslTransform = New System.Xml.Xsl.XslTransform 
   xslt.Load(Me.myXslDoc, resolver, Me.GetType().Assembly.Evidence())        
   xslt.Transform(oXPathNav, args, swContent, Nothing)                       
 Catch ex As Exception
   Debug.WriteLine("Exception: {0}", ex.ToString())
 End Try

 DoSomething(sbContent.ToString())

End Sub

' ----------------------------------------------------------------------

in my old code I have both xmlDoc and xslDoc, so the compiled code I have now (not tested yet) is:

' ----------------------------------------------------------------------
' VS 2008 trial code:

' ----------------------------------------------------------------------

  Dim sbContent As System.Text.StringBuilder = New System.Text.StringBuilder()
  Dim swContent As System.IO.StringWriter = New System.IO.StringWriter(sbContent)
  Dim contentWriter As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(swContent)

  Dim args As New System.Xml.Xsl.XsltArgumentList()
  args.AddParam("paramName1", "", paramVal1.ToString)
  args.AddParam("paramName2", "", paramVal2.ToString)

  Try
    Dim resolver As New System.Xml.XmlUrlResolver
    resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

    Dim evidence As System.Security.Policy.Evidence = Me.GetType().Assembly.Evidence
    Dim secureResolver As New System.Xml.XmlSecureResolver(resolver, evidence)
    Dim settings As New System.Xml.Xsl.XsltSettings(False, True)

    Dim xslt As New System.Xml.Xsl.XslCompiledTransform()  
    xslt.Load(Me.myXslDoc, settings, secureResolver)    
    xslt.Transform(xmlDoc, args, contentWriter)  
  Catch ex As Exception
    Debug.WriteLine("Exception: {0}", ex.ToString())
  End Try

  DoSomething(sbContent.ToString())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-09-01 10:47:34

这些额外的参数不再由 XsltCompiledTransform 类直接公开。您现在可以使用您喜欢的任何 XmlReader 加载转换。

如果您需要额外的安全性,请创建一个使用 XmlSecureResolverXmlReader

Private MyXslDoc As String = "XslFilePath"
Public Sub LoadTransform(ByVal paramVal1 As Object, ByVal paramVal2 As Object)
    Dim args As New System.Xml.Xsl.XsltArgumentList
    args.AddParam("paramName1", "", paramVal1.ToString)
    args.AddParam("paramName2", "", paramVal2.ToString)

    Dim resolver As New System.Xml.XmlUrlResolver
    resolver.Credentials = System.Net.CredentialCache.DefaultCredentials
    Dim evidence As Evidence = Me.GetType().Assembly.Evidence
    Dim secureResolver As New XmlSecureResolver(resolver, evidence)
    Dim settings As New XmlReaderSettings
    settings.XmlResolver = secureResolver

    Dim xslt As New System.Xml.Xsl.XslCompiledTransform()
    Using transformReader As XmlReader = XmlReader.Create(Me.MyXslDoc, settings)
        xslt.Load(transformReader)
    End Using

    Using documentReader As XmlReader = XmlReader.Create(documentUri)
        Dim sbContent As New System.Text.StringBuilder
        Using swContent As New System.IO.StringWriter(sbContent)

            Using contentWriter As XmlWriter = XmlWriter.Create(swContent)
                xslt.Transform(documentReader, args, contentWriter, Nothing)
            End Using
        End Using
    End Using
End Sub

Those extra parameters are no longer exposed directly by the XsltCompiledTransform class. You can now load the transform using any XmlReader that you like.

If you need the extra security, then create an XmlReader that uses an XmlSecureResolver:

Private MyXslDoc As String = "XslFilePath"
Public Sub LoadTransform(ByVal paramVal1 As Object, ByVal paramVal2 As Object)
    Dim args As New System.Xml.Xsl.XsltArgumentList
    args.AddParam("paramName1", "", paramVal1.ToString)
    args.AddParam("paramName2", "", paramVal2.ToString)

    Dim resolver As New System.Xml.XmlUrlResolver
    resolver.Credentials = System.Net.CredentialCache.DefaultCredentials
    Dim evidence As Evidence = Me.GetType().Assembly.Evidence
    Dim secureResolver As New XmlSecureResolver(resolver, evidence)
    Dim settings As New XmlReaderSettings
    settings.XmlResolver = secureResolver

    Dim xslt As New System.Xml.Xsl.XslCompiledTransform()
    Using transformReader As XmlReader = XmlReader.Create(Me.MyXslDoc, settings)
        xslt.Load(transformReader)
    End Using

    Using documentReader As XmlReader = XmlReader.Create(documentUri)
        Dim sbContent As New System.Text.StringBuilder
        Using swContent As New System.IO.StringWriter(sbContent)

            Using contentWriter As XmlWriter = XmlWriter.Create(swContent)
                xslt.Transform(documentReader, args, contentWriter, Nothing)
            End Using
        End Using
    End Using
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文