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. Web Development
  3. ASP.NET
  4. Converting XML to HTML

Converting XML to HTML

Scheduled Pinned Locked Moved ASP.NET
xmlhelpcsharphtml
3 Posts 2 Posters 0 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.
  • J Offline
    J Offline
    John Honan
    wrote on last edited by
    #1

    I have an XML file which has the XSL file specified in the first line: 1380852 …. And the rest of the XML data follows …. I need to transform and output this as HTML. How can this be achieved in ASP.NET / VB.NET ? I have tried using the xmlTransform class, but it doesn’t seem to like the XSL reference embedded in the XML file. E.g: Dim myXslTransform As XslTransform Dim myXPathDocument As XPathDocument myXPathDocument = New XPathDocument("C:\test\test99.xml") myXslTransform = New XslTransform() ' myXslTransform.Load("") 'I don't have a separate .XSL stylesheet... Dim stWrite As System.IO.StringWriter = New System.IO.StringWriter() myXslTransform.Transform(myXPathDocument, Nothing, stWrite) Response.Write(stWrite.ToString) Gives the following error: 'xml:stylesheet' is an invalid name for a processing instruction. Line 1, position 3. Changing the first line of the XML file to read (dash instead of colon) gets rid of this error, but then I no longer have the XSL link, and it just outputs ‘raw’ XML! My end goal is to be able to read in multiple XML files in this format and output them to ONE browser page as HTML. Any help is much appreciated. John.

    Richard DeemingR 1 Reply Last reply
    0
    • J John Honan

      I have an XML file which has the XSL file specified in the first line: 1380852 …. And the rest of the XML data follows …. I need to transform and output this as HTML. How can this be achieved in ASP.NET / VB.NET ? I have tried using the xmlTransform class, but it doesn’t seem to like the XSL reference embedded in the XML file. E.g: Dim myXslTransform As XslTransform Dim myXPathDocument As XPathDocument myXPathDocument = New XPathDocument("C:\test\test99.xml") myXslTransform = New XslTransform() ' myXslTransform.Load("") 'I don't have a separate .XSL stylesheet... Dim stWrite As System.IO.StringWriter = New System.IO.StringWriter() myXslTransform.Transform(myXPathDocument, Nothing, stWrite) Response.Write(stWrite.ToString) Gives the following error: 'xml:stylesheet' is an invalid name for a processing instruction. Line 1, position 3. Changing the first line of the XML file to read (dash instead of colon) gets rid of this error, but then I no longer have the XSL link, and it just outputs ‘raw’ XML! My end goal is to be able to read in multiple XML files in this format and output them to ONE browser page as HTML. Any help is much appreciated. John.

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

      The correct declaration is: <?xml-stylesheet type="text/xsl" href="_URI of your stylesheet_"?> If you open the file in Internet Explorer, you will see the result of the transformation. The XslTransform class will not detect the stylesheet from the processing instruction. You will have to extract the URI and load the stylesheet yourself. For example:

      Imports System.Xml.XPath
      Imports System.Text.RegularExpressions
      ...
      Public Shared Function GetStylesheet(ByVal doc As IXPathNavigable) As String
      If doc Is Nothing Then Return String.Empty

      Dim nav As XPathNavigator = doc.CreateNavigator()
      If Not nav.MoveToFirstChild() Then Return String.Empty
      
      Dim href As New Regex(" href=\['""\](?<href>\[^'""\]\*)\['""\]", \_
          RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)
      
      Do
          If XPathNodeType.ProcessingInstruction = nav.NodeType \_
              AndAlso "xml-stylesheet" = nav.Name Then
          
              Dim m As Match = href.Match(nav.Value)
              If Not m Is Nothing Then
                  Return m.Groups("href").Captures(0).Value
              End If
          End If
      While nav.MoveToNext()
      
      Return String.Empty
      

      End Function
      ...
      Dim myXPathDocument As New XPathDocument("C:\test\test99.xml")
      Dim myXslTransform As New XslTransform()
      myXslTransform.Load(GetStylesheet(myXPathDocument))
      Dim stWrite As New System.IO.StringWriter()
      myXslTransform.Transform(myXPathDocument, Nothing, stWrite)
      Response.Write(stWrite.ToString)


      "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

      J 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        The correct declaration is: <?xml-stylesheet type="text/xsl" href="_URI of your stylesheet_"?> If you open the file in Internet Explorer, you will see the result of the transformation. The XslTransform class will not detect the stylesheet from the processing instruction. You will have to extract the URI and load the stylesheet yourself. For example:

        Imports System.Xml.XPath
        Imports System.Text.RegularExpressions
        ...
        Public Shared Function GetStylesheet(ByVal doc As IXPathNavigable) As String
        If doc Is Nothing Then Return String.Empty

        Dim nav As XPathNavigator = doc.CreateNavigator()
        If Not nav.MoveToFirstChild() Then Return String.Empty
        
        Dim href As New Regex(" href=\['""\](?<href>\[^'""\]\*)\['""\]", \_
            RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)
        
        Do
            If XPathNodeType.ProcessingInstruction = nav.NodeType \_
                AndAlso "xml-stylesheet" = nav.Name Then
            
                Dim m As Match = href.Match(nav.Value)
                If Not m Is Nothing Then
                    Return m.Groups("href").Captures(0).Value
                End If
            End If
        While nav.MoveToNext()
        
        Return String.Empty
        

        End Function
        ...
        Dim myXPathDocument As New XPathDocument("C:\test\test99.xml")
        Dim myXslTransform As New XslTransform()
        myXslTransform.Load(GetStylesheet(myXPathDocument))
        Dim stWrite As New System.IO.StringWriter()
        myXslTransform.Transform(myXPathDocument, Nothing, stWrite)
        Response.Write(stWrite.ToString)


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

        J Offline
        J Offline
        John Honan
        wrote on last edited by
        #3

        Thanks for the reply and the code Richard - I like the 'GetStyleSheet()' function! - I have a feeling you've been through this before? :-) The XML files I'm using were created by a different Dev team, I have no idea why they are using

        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