cdn 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
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 -
<?xml version="1.0" encoding="utf-8" ?>
-
<docroot>
-
<products>
-
<product productcode="0001">
-
<name>prod1</name>
-
<colour>black</colour>
-
</product>
-
<product productcode="0002">
-
<name>prod2</name>
-
<colour>grey</colour>
-
</product>
-
</products>
-
</docroot>
-
and xslt transform.xslt: -
<?xml version="1.0" encoding="UTF-8" ?>
-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-
<xsl:template match="/">
-
<table>
-
<thead>
-
<tr>
-
<th>Product Name</th>
-
<th>Product Code</th>
-
<th>Product Colour</th>
-
</tr>
-
</thead>
-
<tbody>
-
<xsl:apply-templates />
-
</tbody>
-
</table>
-
</xsl:template>
-
-
<xsl:template match="//product">
-
<tr>
-
<td>
-
<xsl:value-of select="name"/>
-
</td>
-
<td>
-
<xsl:value-of select="@productcode"/>
-
</td>
-
<td>
-
<xsl:value-of select="colour"/>
-
</td>
-
</tr>
-
</xsl:template>
-
</xsl:stylesheet>
-
The following code will return a string containing the HTML table code: -
Imports System.Xml
-
Imports System.Xml.Xsl
-
Imports System.Text
-
-
Public Function XsltDemo() As String
-
Dim doc As New XmlDocument
-
Dim xr As XmlReader
-
Dim xmlTransform As New XslCompiledTransform()
-
-
'loadXml, if this is already loaded or in a string ignore this step
-
doc.Load(AppDomain.CurrentDomain.BaseDirectory & "small.xml")
-
-
Dim builder As New StringBuilder
-
Dim tr As New System.IO.StringWriter(builder)
-
Dim xtw As New XmlTextWriter(tr)
-
-
Dim ioFile As IO.FileStream
-
-
'Check for transform in cache.
-
If Context.Cache.Item("Xsl") Is Nothing Then
-
'load the transform into the filestream so that is can be read into an XmlReader
-
'overloads are used so that there are not access errors due to multiple requests
-
ioFile = IO.File.Open(AppDomain.CurrentDomain.BaseDirectory & "transform.xslt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
-
-
'Read the transform into the reader
-
xr = XmlReader.Create(ioFile)
-
-
'Load the reader into the transform
-
xmlTransform.Load(xr)
-
-
'adds transform to cache for next load. Cached for 1 day
-
Context.Cache.Add("Xsl", xmlTransform, Nothing, Cache.NoAbsoluteExpiration, (New TimeSpan(1, 0, 0)), CacheItemPriority.Normal, Nothing)
-
Else
-
load cached transform
-
xmlTransform = Context.Cache.Item("Xsl")
-
End If
-
-
'apply transform to the string representing the XML document
-
'use the OuterXml if the XML is in an XmlDocument object
-
'transform result is written to the XmlTextWriter xtw.
-
xmlTransform.Transform((New XmlTextReader(doc.OuterXml, XmlNodeType.Document, Nothing)), xtw)
-
-
'Flush the XmlTextWriter so that the string builder is populated
-
xtw.Flush()
-
'Close the XmlTextWriter and underlying StringWriter
-
xtw.Close()
-
'Return the builder and HTML code
-
Return builder.ToString
-
End Function
-
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.
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
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: -
using System;
-
using System.Xml;
-
using System.Xml.Xsl;
-
using System.Text;
-
using System.IO;
-
-
public class Class1
-
{
-
-
public String XsltDemo(){
-
XmlDocument doc = new XmlDocument();
-
XmlReader xr = null;
-
XslCompiledTransform xmlTransform = new XslCompiledTransform();
-
-
//loadXml, if this is already loaded or in a string ignore this step
-
doc.Load(AppDomain.CurrentDomain.BaseDirectory & "small.xml");
-
-
StringBuilder builder = new StringBuilder();
-
StringWriter tr = new StringWriter(builder);
-
XmlTextWriter xtw = new XmlTextWriter(tr);
-
-
FileStream ioFile = null;
-
-
//Check for transform in cache.
-
if (Context.Cache.Item("Xsl") = null){
-
//load the transform into the filestream so that is can be read into an XmlReader
-
//overloads are used so that there are not access errors due to multiple requests
-
ioFile = File.Open(AppDomain.CurrentDomain.BaseDirectory & "transform.xslt", FileMode.Open, FileAccess.Read, FileShare.Read);
-
-
//read the transform into the reader
-
xr = XmlReader.Create(ioFile);
-
-
//Load the reader into the transform
-
xmlTransform.Load(xr);
-
-
//adds transform to cache for next load. Cached for 1 day
-
Context.Cache.Add("Xsl", xmlTransform, Nothing, Cache.NoAbsoluteExpiration, (new TimeSpan(1, 0, 0)),
-
CacheItemPriority.Normal, null);
-
}else{
-
//load cached transform
-
xmlTransform = Context.Cache.Item("Xsl");
-
}
-
-
//apply transform to the string representing the XML document
-
//use the OuterXml if the XML is in an XmlDocument object
-
//transform result is written to the XmlTextWriter xtw.
-
xmlTransform.Transform((new XmlTextReader(doc.OuterXml, XmlNodeType.Document, null)), xtw);
-
-
//Flush the XmlTextWriter so that the string builder is populated
-
xtw.Flush();
-
//Close the XmlTextWriter and underlying StringWriter
-
xtw.Close();
-
//Return the builder and HTML code
-
return builder.ToString;
-
}
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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:
...
|
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...
| |
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...
|
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....
|
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.
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
| |