473,513 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do i formate attribute of xml into Html table

2 New Member
hii
i have stream of xml and i had display it's content in aspx page.All the content are displating inside a label of asp.net. But i want to display all the content inside a html table so that it can aligned it proper manner. Plzz help me

Thank You in Advance..
Mitesh
Aug 16 '07 #1
3 3188
phvfl
173 Recognized Expert New Member
Hi Mitesh,

If you are providing a consistent formatting across the entire XML document then the quickest method would be using XSLT. Using this you could transform the XML document into a HTML table. In .NET you would be using the XslCompiledTransform class of the System.Xml.Xsl namespace.

Example:
Using small.xml
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <docroot>
  3.   <products>
  4.     <product productcode="0001">
  5.       <name>prod1</name>
  6.       <colour>black</colour>
  7.     </product>
  8.     <product productcode="0002">
  9.       <name>prod2</name>
  10.       <colour>grey</colour>
  11.     </product>
  12.   </products> 
  13. </docroot>
  14.  
and xslt transform.xslt:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3.   <xsl:template match="/">
  4.     <table>
  5.       <thead>
  6.         <tr>
  7.           <th>Product Name</th>
  8.           <th>Product Code</th>
  9.           <th>Product Colour</th>
  10.         </tr>
  11.       </thead>
  12.       <tbody>
  13.         <xsl:apply-templates />
  14.       </tbody>
  15.     </table>
  16.   </xsl:template>
  17.  
  18.   <xsl:template match="//product">
  19.     <tr>
  20.       <td>
  21.         <xsl:value-of select="name"/>
  22.       </td>
  23.       <td>
  24.         <xsl:value-of select="@productcode"/>
  25.       </td>
  26.       <td>
  27.         <xsl:value-of select="colour"/>
  28.       </td>
  29.     </tr>
  30.   </xsl:template>
  31. </xsl:stylesheet>
  32.  
The following code will return a string containing the HTML table code:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Xml
  2. Imports System.Xml.Xsl
  3. Imports System.Text
  4.  
  5.     Public Function XsltDemo() As String
  6.         Dim doc As New XmlDocument
  7.         Dim xr As XmlReader
  8.         Dim xmlTransform As New XslCompiledTransform()
  9.  
  10.         'loadXml, if this is already loaded or in a string ignore this step
  11.         doc.Load(AppDomain.CurrentDomain.BaseDirectory & "small.xml")
  12.  
  13.         Dim builder As New StringBuilder
  14.         Dim tr As New System.IO.StringWriter(builder)
  15.         Dim xtw As New XmlTextWriter(tr)
  16.  
  17.         Dim ioFile As IO.FileStream
  18.  
  19.         'Check for transform in cache.
  20.         If Context.Cache.Item("Xsl") Is Nothing Then
  21.             'load the transform into the filestream so that is can be read into an XmlReader
  22.             'overloads are used so that there are not access errors due to multiple requests
  23.             ioFile = IO.File.Open(AppDomain.CurrentDomain.BaseDirectory & "transform.xslt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
  24.  
  25.             'Read the transform into the reader
  26.             xr = XmlReader.Create(ioFile)
  27.  
  28.             'Load the reader into the transform
  29.             xmlTransform.Load(xr)
  30.  
  31.             'adds transform to cache for next load. Cached for 1 day
  32.             Context.Cache.Add("Xsl", xmlTransform, Nothing, Cache.NoAbsoluteExpiration, (New TimeSpan(1, 0, 0)), CacheItemPriority.Normal, Nothing)
  33.         Else
  34.         load cached transform
  35.             xmlTransform = Context.Cache.Item("Xsl")
  36.         End If
  37.  
  38.         'apply transform to the string representing the XML document
  39.         'use the OuterXml if the XML is in an XmlDocument object
  40.         'transform result is written to the XmlTextWriter xtw.
  41.         xmlTransform.Transform((New XmlTextReader(doc.OuterXml, XmlNodeType.Document, Nothing)), xtw)
  42.  
  43.         'Flush the XmlTextWriter so that the string builder is populated
  44.         xtw.Flush()
  45.         'Close the XmlTextWriter and underlying StringWriter
  46.         xtw.Close()
  47.         'Return the builder and HTML code
  48.         Return builder.ToString
  49.     End Function
  50.  
The result is:[HTML]
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Product Code</th>
<th>Product Colour</th>
</tr>
</thead>
<tbody>
<tr>
<td>prod1</td>
<td>0001</td>
<td>black</td>
</tr>
<tr>
<td>prod2</td>
<td>0002</td>
<td>grey</td>
</tr>
</tbody>
</table>[/HTML]

If you are putting a small amount of the XML document into a table then it may be easier to extract the information from the document and manually build the table, either as a table server control and add to a placeholder or build the HTML code in a string builder and display the code in a literal control.
Aug 18 '07 #2
miamikk
6 New Member
In regards to your reply posted here,
http://www.thescripts.com/forum/thread693679.html

Do you have the code in C# ?

I have XML files that I need to display as HTML tables.

The XML file looks like http://www.fiu.edu/~atmakurk/2.xml

And the table more or less should like http://www.fiu.edu/~atmakurk/Table_View1.jpg

I would appreciate if can provide with an example of converting XML to HTML using XSLT in ASP.NET using C#.

Thanks
Oct 24 '07 #3
phvfl
173 Recognized Expert New Member
In regards to your reply posted here,
http://www.thescripts.com/forum/thread693679.html

Do you have the code in C# ?

I have XML files that I need to display as HTML tables.

The XML file looks like http://www.fiu.edu/~atmakurk/2.xml

And the table more or less should like http://www.fiu.edu/~atmakurk/Table_View1.jpg

I would appreciate if can provide with an example of converting XML to HTML using XSLT in ASP.NET using C#.

Thanks
I have converted the VB.NET code above to C# to be:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Xml;
  3. using System.Xml.Xsl;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. public class Class1
  8. {
  9.  
  10.     public String XsltDemo(){
  11.       XmlDocument doc = new XmlDocument();
  12.         XmlReader xr = null;
  13.         XslCompiledTransform xmlTransform = new XslCompiledTransform();
  14.  
  15.         //loadXml, if this is already loaded or in a string ignore this step
  16.         doc.Load(AppDomain.CurrentDomain.BaseDirectory & "small.xml");
  17.  
  18.         StringBuilder builder = new StringBuilder();
  19.         StringWriter tr = new StringWriter(builder);
  20.         XmlTextWriter xtw = new XmlTextWriter(tr);
  21.  
  22.         FileStream ioFile = null;
  23.  
  24.         //Check for transform in cache.
  25.         if (Context.Cache.Item("Xsl") = null){
  26.           //load the transform into the filestream so that is can be read into an XmlReader
  27.           //overloads are used so that there are not access errors due to multiple requests
  28.           ioFile = File.Open(AppDomain.CurrentDomain.BaseDirectory   & "transform.xslt", FileMode.Open, FileAccess.Read, FileShare.Read);
  29.  
  30.           //read the transform into the reader
  31.           xr = XmlReader.Create(ioFile);
  32.  
  33.           //Load the reader into the transform
  34.           xmlTransform.Load(xr);
  35.  
  36.           //adds transform to cache for next load. Cached for 1 day
  37.           Context.Cache.Add("Xsl", xmlTransform, Nothing, Cache.NoAbsoluteExpiration, (new TimeSpan(1, 0, 0)), 
  38.           CacheItemPriority.Normal, null);
  39.         }else{
  40.           //load cached transform
  41.           xmlTransform = Context.Cache.Item("Xsl");
  42.         }
  43.  
  44.         //apply transform to the string representing the XML document
  45.         //use the OuterXml if the XML is in an XmlDocument object
  46.         //transform result is written to the XmlTextWriter xtw.
  47.         xmlTransform.Transform((new XmlTextReader(doc.OuterXml, XmlNodeType.Document, null)), xtw);
  48.  
  49.         //Flush the XmlTextWriter so that the string builder is populated
  50.         xtw.Flush();
  51.         //Close the XmlTextWriter and underlying StringWriter
  52.         xtw.Close();
  53.         //Return the builder and HTML code
  54.         return builder.ToString;
  55.     }    
  56. }
  57.  

Providing that you have your XSLT you should be able to perform the transformation.

If you are new to XSLT there is a good tutorial at w3schools. If you are having trouble with the transformation please post what you have attempted.
Oct 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2863
by: piraticman | last post by:
Hi. I have some code for a javascript jump menu... It validated with HTML 4.0 but now does not validate with XHTML strict 1.0. I have used name atributes in my code which aparantly are not...
33
3254
by: bissatch | last post by:
Hi, I fully understand the purpose of an alt attribute within a <img> tag but why would you use a title or summary attribute within, for example, a <p> tag. I have read books recommending that I...
4
3191
by: Unknown | last post by:
When I create a Table webcontrol programmatically (no static table tag in the aspx file), it renders in the HTML with a border attribute set to "0". If I add my own border...
3
1986
by: Bill Endow | last post by:
I've noticed that the WebControl.Table class always outputs the attribute "border="0"", and I cannot stop it from doing so, no matter what I try. I end up with generated HTML that looks like: ...
1
1217
by: Gürkan Demirci | last post by:
Hi, i am using the VisualStudio FormDesigner to create an asp:table. I want to populate an asp:tablecell with different controls at runtime. In the codebehind file, there is an attribute for the...
1
2185
by: Art | last post by:
Not sure whether the question is appropriate to the .html and/or ..stylesheets ng so I'm posting to both. I have been trying to find a solution to a problem regarding adding of the padding...
11
5811
by: Jasbird | last post by:
Has the name attribute deprecated? I ask this because ASP.NET 2 warns me against using it, says that it has been deprecated and doesn't use it (on the client) when creating a radio button list....
3
1986
by: raahat | last post by:
hi, i need to do desperately the way of storing american DATE formate( like 27-02-2007) in mysql table.i searched on the web a lot,but didnt find anything.PLZ i need someone's help.
2
1429
by: phpfreak2007 | last post by:
Hi everyone... I want to copy text from clipboard and paste into my project.I am able to do it but problem is Its not in same formate..That means I want bold letter as bold and with same...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.