Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Transform XML

Transform XML

Scheduled Pinned Locked Moved XML / XSL
xmlsysadmin
3 Posts 2 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TML
    wrote on last edited by
    #1

    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

    Richard DeemingR 1 Reply Last reply
    0
    • T TML

      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

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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 new StringWriter 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      T 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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 new StringWriter 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

        T Offline
        T Offline
        TML
        wrote on last edited by
        #3

        If it was a snake, it would have bit me. Thanks Richard!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups