p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/forumdisplay.php?f=215)
-   -   Converting a given string to html table in asp,net (http://p2p.wrox.com/showthread.php?t=61880)

ostwald September 8th, 2007 01:20 PM

Converting a given string to html table in asp,net
 
Hi friends,
Can anyone help me abt this application.
I'm giving a html table string in a text box.(ex: <table><tr><td> ...............</tr></table>) as input.
And it must return me a datatable as an output and the values must be stored in database.
Plz help me this.

regards
Ossy!

planoie September 9th, 2007 07:14 PM

If the HTML is XHTML compliant you could treat it as XML and get the rows and values from the table using an xml document and the right set of xpath queries. This would be a fairly elegant way to do it and would probably require the least amount of code. If you can't expect the input to conform to XHTML then you'll have to write a parser to handle more poorly formed input.

-Peter

ostwald September 10th, 2007 04:44 AM

Hey Pete,
Thanx fo ur reply. I achieved the application by parsing. I didn'y use XHTML. I used html text as string and then parsed it to generate the required criteria. Herez the code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" ValidateRequest="false" %>
<%@ Import namespace="System.Text" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<script language="C#" type="text/C#" runat=server >
private void Post_Text(object sender, EventArgs e)
{

if(mess.Text!="")
{

if(parse.Checked)
{

if(htmlpost.Checked)
{

postmess.Text=parsetext(mess.Text,false) ;
}
else
{

//postmess.Text=parsetext(mess.Text,true) ;
//}
//}
//else
//{

postmess.Text=mess.Text ;
String s = "Select * from htmln";
SqlConnection con = new SqlConnection("Data Source=KIRAN;Initial Catalog=TrylahNewLayout;Persist Security Info=True;User ID=sa;Password=sa123");
SqlDataAdapter da = new SqlDataAdapter(s,con);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet("htmln");
da.Fill(ds, "htmln");

DataRow row = ds.Tables["htmln"].NewRow();
row["doc"] = postmess.Text;
ds.Tables["htmln"].Rows.Add(row);
da.Update(ds, "htmln");



}
}

public string parsetext(string text, bool allow)
{

StringBuilder sb = new StringBuilder(text) ;

sb.Replace(" "," &nbsp;");

if(!allow)
{

sb.Replace("<","&lt;") ;
sb.Replace(">","&gt;") ;

sb.Replace("\"","&quot;");
}

StringReader sr = new StringReader(sb.ToString());
StringWriter sw = new StringWriter();

while(sr.Peek()>-1)
{

string temp = sr.ReadLine();

sw.Write(temp+"<br>") ;
}

return sw.GetStringBuilder().ToString();
}
</script>
<body>


<center>
<br>
<form runat=server ID="Form2">
<table border=1>
<tr>
<td valign=top>Result</td>
<td>
<asp:label text="&nbsp;" id=postmess runat=server Width="68px" />
</td></tr>
<tr><td valign=top>Enter Message </td>
<td><asp:textbox Columns="50" Rows="20" TextMode="MultiLine" id=mess runat=server /></td></tr>
<tr><td colspan=2 style="height: 28px"><asp:button onClick="Post_Text" text="Generate!" runat=server ID="Button1" /></td></tr>
</table>
</form>
</center>
</body>
</html>


All times are GMT -4. The time now is 02:39 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.