- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有以下代码需要从 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但是在我的代码中我看到一些注释暗示添加的参数是用来避免异常的,所以我更喜欢使用这些参数。
有人可以帮忙迁移这段代码吗?
谢谢,阿塔拉
' ----------------------------------------------------------------------
' 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())
最佳答案
XsltCompiledTransform
类不再直接公开这些额外参数。您现在可以使用您喜欢的任何 XmlReader
加载转换。
如果您需要额外的安全性,则创建一个使用 XmlSecureResolver
的 XmlReader
:
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
关于xml - 如何从 XslTransform 迁移到 XslCompiledTransform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2442252/
XslTransform 似乎已被 Microsoft 弃用,而支持 XslCompiledTransform。从理论上讲,如果我在应用程序执行期间只进行一次转换,那么解释 XSLT(通过 XslTr
那么一旦您启动了长时间运行的 XslTransform.Transform,您将如何中止它? XslTransform XslTransform = new XslTransform();
我有以下代码: using (Stream stream = new MemoryStream()) { xslt.Transform(docu
我在一个旧程序中使用了 XslTransform,在将代码转换为 .NET F 3.5 之后,编译器表示 XslTransform 已被弃用并被 XslCompiledTransform 取代。 这是
我正在修改一些遗留代码以尝试消除警告。 XmlDataDocument 和 XslTransform 都会生成它们已过时的警告。对于 XslTransform,建议的替换是 XslCompiledTr
貌似大家都说如果用XslTransform,会先调用Load加载样式表,再调用Transform进行转换。但是我有以下 XML 文件: .... 我应该先加载 xml,找到样式表节点,然后调用
我有以下代码需要从 VS 2003 (.Net 1.1) 迁移到 VS 2008 (.Net 2+)但我收到编译错误 - System.Xml.Xsl.XslTransform' 已过时:此类已被弃用
我有一个 XML 文件,我想将其解析为基于 XLST 文件的非 XML(文本)文件。两者中的代码似乎都是正确的,并且在手动测试时可以正常工作,但是我在以编程方式执行此操作时遇到了问题。 我正在使用 J
我正在使用 Microsoft Word 文件中的 XSLT 1.0 转换 XML 文件。 这是我的 XSLT 1.0: ....................
所以有一段时间我一直在使用 XslCompiledTransform,因为这是 Microsoft 告诉我需要使用的,因为 XslTransform 已被弃用。最近我不得不将它用于具有近 100,00
我是一名优秀的程序员,十分优秀!