如何从XslTransform迁移到XslCompiledTransform

时间:2010-03-14 13:21:56

标签: xml vb.net xmldocument xslcompiledtransform

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

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

MSDN中的

[如何:迁移您的XslTransform代码]我只发现了一些更简单的案例。 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())

1 个答案:

答案 0 :(得分:2)

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

如果您需要额外的安全性,请创建使用XmlReader的{​​{1}}:

XmlSecureResolver