2010-03-14 61 views
4

我有下面的代碼,我需要從VS 2003(.NET 1.1)遷移到VS 2008(.NET 2+) 但我得到的編譯錯誤 -如何從遷移到的XslTransform XslCompiledTransform

系統。 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 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()) 

回答

2

那些額外的參數不再直接暴露在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 
+0

感謝。 以及如何在任何xslt.Load()重載中使用XmlDocument? – Atara 2010-03-14 14:19:14

+0

@Atara:在此工作 – 2010-03-14 14:23:41

+0

@Atara:檢查代碼。我沒有測試過它,但是它建立了。 – 2010-03-14 14:51:15