Transform XML
-
I am trying to transform an XML file through code using XSL, but for some reason, the file output is displaying the entire XML as a string at the top of the document and then, just under the XML string, the document is parsed correctly by the XSL stylesheet. I would like to learn why this is happening. Here is the VB transform code:
Dim ds As New DataSet ds.ReadXml(Server.MapPath("FormResults/" & FormID.ToString() & ".xml")) Dim DocXSL As New XslCompiledTransform Dim sw As New StringWriter() ds.WriteXml(sw) ds.Dispose() Dim DocXML As New XmlDocument DocXML.LoadXml(sw.ToString()) 'The GetXSLContent function returns the XLS template to be used DocXSL.Load(GetXSLContent(DFLID)) DocXSL.Transform(DocXML, Nothing, sw) Dim result As String = sw.ToString() sw.Close() sw = Nothing
Me.My_Literal.Text = result.ToString()
Private Function GetXSLContent(ByVal DFLID As Integer) As String Dim str As String = String.Empty str = Server.MapPath("FormLetters/" & DFLID.ToString() & "\_DataTemplate.xslt") Return str End Function
Here is the XML
I.T. 2267 Primary Office Bldg David 9/12/2014 04:49 This is a test. asdf sasdf sa This is only a test. This is a test. This is only a test. This is a test. This is only a test. This is a test. This is only a test.
Here is the XSLT
-
I am trying to transform an XML file through code using XSL, but for some reason, the file output is displaying the entire XML as a string at the top of the document and then, just under the XML string, the document is parsed correctly by the XSL stylesheet. I would like to learn why this is happening. Here is the VB transform code:
Dim ds As New DataSet ds.ReadXml(Server.MapPath("FormResults/" & FormID.ToString() & ".xml")) Dim DocXSL As New XslCompiledTransform Dim sw As New StringWriter() ds.WriteXml(sw) ds.Dispose() Dim DocXML As New XmlDocument DocXML.LoadXml(sw.ToString()) 'The GetXSLContent function returns the XLS template to be used DocXSL.Load(GetXSLContent(DFLID)) DocXSL.Transform(DocXML, Nothing, sw) Dim result As String = sw.ToString() sw.Close() sw = Nothing
Me.My_Literal.Text = result.ToString()
Private Function GetXSLContent(ByVal DFLID As Integer) As String Dim str As String = String.Empty str = Server.MapPath("FormLetters/" & DFLID.ToString() & "\_DataTemplate.xslt") Return str End Function
Here is the XML
I.T. 2267 Primary Office Bldg David 9/12/2014 04:49 This is a test. asdf sasdf sa This is only a test. This is a test. This is only a test. This is a test. This is only a test. This is a test. This is only a test.
Here is the XSLT
TML wrote:
Dim sw As New StringWriter()
ds.WriteXml(sw)
...
DocXSL.Transform(DocXML, Nothing, sw)
Dim result As String = sw.ToString()You've declared a single
StringWriter
instance. You've written the XML to it. Then you've written the results of the transformation to it. It's hardly surprising that it then contains both the XML and the results of the transformation. :) After loading the XML, you either need to create a newStringWriter
instance, or you need to clear the existing one....
DocXML.LoadXml(sw.ToString())
' Create a new instance:
' sw = New StringWriter()' OR: Clear the existing one:
sw.GetStringBuilder().Clear()...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
TML wrote:
Dim sw As New StringWriter()
ds.WriteXml(sw)
...
DocXSL.Transform(DocXML, Nothing, sw)
Dim result As String = sw.ToString()You've declared a single
StringWriter
instance. You've written the XML to it. Then you've written the results of the transformation to it. It's hardly surprising that it then contains both the XML and the results of the transformation. :) After loading the XML, you either need to create a newStringWriter
instance, or you need to clear the existing one....
DocXML.LoadXml(sw.ToString())
' Create a new instance:
' sw = New StringWriter()' OR: Clear the existing one:
sw.GetStringBuilder().Clear()...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer