Hi All, here a lwebode's C# class to generate a Feed rss 2.0 ,
it can
generate by code or by SQL with dataset create a new class naming it
“RSS_Writer” and just copy and paste code below.
//LWEBCODE C# RSS 2.0 Atom FEED Writer Usage example:
//To generate a feed by code (in this case rss will placed into root directory of websites):
string RssPath = Server.MapPath("../rss.xml");
RSS_Writer.NewRss(RssPath, "FEED Title", "www.exmple.com", "Header Description of feed", "en-us");
//to add a items programmatically:
RSS_Writer.AddItems(RssPath, "your title", "link to this article", "Description", "");
//to generate rss from SQL after have filled a Dataset (ds) :
RSS_Writer.CreateRssFromDataSet(RssPath, ds, "titleFiled", "LnkField", "DescrField", "GuidField");
//Here C# LWEBCODE RSS 2.0 Atom FEED Writer Class
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
/// RSS_Writer
/// generated by LWEBCODE
/// http://lwebcode.blogspot.com/
///
///
public class RSS_Writer
{
public RSS_Writer()
{
//string path = HttpContext.Current.Server.MapPath("rss.xml");
public static int NewRss(string path, string sTitle, string sLink, string sDescription, string sLanguage)
{
//XmlTextWriter writer = OpenXMLWriter(path);
/*writer.WriteStartDocument();
writer.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='lead.xsl'");
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteStartElement("channel");
writer.WriteElementString("title", sTitle);
writer.WriteElementString("link", sLink);
writer.WriteElementString("description", sDescription);
writer.WriteElementString("language", sLanguage);
string GMTDate = System.TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now).ToString("r");
writer.WriteElementString("lastBuildDate", GMTDate);
*/
XmlDocument xd = new XmlDocument();
//xd.Load(path);
XmlNode xn;
xn = xd.CreateNode(XmlNodeType.Element, "rss", null);
XmlAttribute xa;
xa = xd.CreateAttribute("version");
xa.Value = "2.0";
xn.Attributes.Append(xa);
xd.AppendChild(xn);
xn = xd.CreateNode(XmlNodeType.Element, "channel", null);
xd.SelectSingleNode("//rss").AppendChild(xn);
xn = xd.CreateNode(XmlNodeType.Element, "title", null);
xn.InnerText = sTitle;
xd.SelectSingleNode("//channel").AppendChild(xn);
xn = xd.CreateNode(XmlNodeType.Element, "link", null);
xn.InnerText = sLink;
xd.SelectSingleNode("//channel").InsertAfter(xn, xd.SelectSingleNode("//title"));
xn = xd.CreateNode(XmlNodeType.Element, "description", null);
xn.InnerText = sDescription;
xd.SelectSingleNode("//channel").InsertAfter(xn, xd.SelectSingleNode("//link"));
xn = xd.CreateNode(XmlNodeType.Element, "language", null);
xn.InnerText = sLanguage;
xd.SelectSingleNode("//channel").InsertAfter(xn, xd.SelectSingleNode("//description"));
string GMTDate = System.TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now).ToString("r");
xn = xd.CreateNode(XmlNodeType.Element, "lastBuildDate", null);
xn.InnerText = GMTDate;
xd.SelectSingleNode("//channel").InsertAfter(xn, xd.SelectSingleNode("//language"));
XmlTextWriter writer = OpenXMLWriter(path, false);
xd.Save(writer);
CloseXMLWriter(writer);
return 0;
}
public static int AddItems(string path, string sTitle, string sLink, string sDescription, string sGuid)
{
UpdateRssHEAD(path);
XmlDocument xd = new XmlDocument();
xd.Load(path);
XmlNode xn = xd.CreateNode(XmlNodeType.Element, "item", null);
XmlNode xn2;
xn2 = xd.CreateNode(XmlNodeType.Element, "title", null);
xn2.InnerText = sTitle;
xn.AppendChild(xn2);
xn2 = xd.CreateNode(XmlNodeType.Element, "link", null);
xn2.InnerText = sLink;
xn.AppendChild(xn2);
xn2 = xd.CreateNode(XmlNodeType.Element, "description", null);
xn2.InnerText = sDescription;
xn.AppendChild(xn2);
string GMTDate = System.TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now).ToString("r");
xn2 = xd.CreateNode(XmlNodeType.Element, "pubDate", null);
xn2.InnerText = GMTDate;
xn.AppendChild(xn2);
xn2 = xd.CreateNode(XmlNodeType.Element, "guid", null);
xn2.InnerText = sGuid;
xn.AppendChild(xn2);
xd.SelectSingleNode("//channel").InsertAfter(xn, xd.SelectSingleNode("//lastBuildDate"));
XmlTextWriter writer = OpenXMLWriter(path, false);
xd.Save(writer);
CloseXMLWriter(writer);
return 0;
}
public static int CreateRssFromDataSet(string path, DataSet ds, string sTitleField, string sLinkField, string sDescriptionField, string sGuidField)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
AddItems(path, ds.Tables[0].Rows[i][sTitleField].ToString(), ds.Tables[0].Rows[i][sLinkField].ToString(), ds.Tables[0].Rows[i][sDescriptionField].ToString(), ds.Tables[0].Rows[i][sGuidField].ToString());
}
return 0;
}
private static int UpdateRssHEAD(string path)
{
XmlDocument xd = new XmlDocument();
xd.Load(path);
XmlNode nodeCh = xd.DocumentElement.SelectSingleNode("//channel");
XmlNode xnd = xd.DocumentElement.SelectSingleNode("//lastBuildDate");
string GMTDate = System.TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now).ToString("r");
xnd.InnerText = GMTDate;
nodeCh.ReplaceChild(xnd, xnd);
XmlTextWriter writer = OpenXMLWriter(path, false);
xd.Save(writer);
CloseXMLWriter(writer);
return 0;
}
private static XmlTextWriter OpenXMLWriter(string path)
{
XmlTextWriter writer;
if (!File.Exists(path))
{
writer = new XmlTextWriter(path, System.Text.Encoding.UTF8);
}
else
{
Stream xmlFile = new System.IO.FileStream(path, FileMode.Append);
writer = new XmlTextWriter(xmlFile, System.Text.Encoding.UTF8);
}
return writer;
}
private static XmlTextWriter OpenXMLWriter(string path, bool Append)
{
XmlTextWriter writer;
if ((!File.Exists(path)) || (!Append))
{
writer = new XmlTextWriter(path, System.Text.Encoding.UTF8);
}
else
{
Stream xmlFile = new System.IO.FileStream(path, FileMode.Append);
writer = new XmlTextWriter(xmlFile, System.Text.Encoding.UTF8);
}
return writer;
}
private static void CloseXMLWriter(XmlTextWriter writer)
{
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
}
/*
IMPORTANT NOTES:
Then to configure IIS, follow these steps:
Open IIS and navigate to the appropriate application/website
Right click and choose ‘Properties’ from the menu
Select the ‘HTTP Headers’ tab
There’s a section at the bottom entitled ‘MIME Map’, and from that click on ‘File Types’
Click ‘New Type’
For the ‘Associated extension’ enter .rss
And for ‘Content type (MIME)’ enter application/rss+xml
Click ‘OK’ and then ‘Apply’
Feel free to restart the IIS server, although you shouldn’t have to.
Ensure that the HTML page link to the RSS file includes the RSS extension, e.g.
*/
}
Hi All, Here's a static class developed in c# to compress Css stylesheets,
It removes all “\r\n”
(newline carriage return) , all “\t” (tab) all spaces before and after
“:” “;” , it also remove all comment from your css.
Thanks to this function
Stylesheet should be more light and fast for web and you can remove additionals
spaces manually for complete optimization .
You can copy your css and pass it to class with:
textBox2.Text = LWEBCODE.Get_CSS_RTM(textBox1.Text);
I suggest to make a form o webform with 2 textboxes and paste in textbox1 your
css stylesheet ,result can be show in textbox2 , after paste into
css_runtime_sheet.css,
REMEMBER on pagae_load event or in <head> tag to correctly set the right stylesheet: version for development and debug, or runtime version
Please for any optimization or suggestion contact us.
Here the code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
/*
Css Runtime Generator, compressor
developed by: lwebcode;
for other script and resource :
http://lwebcode.blogspot.com/
*/
class LWEBCODE
{
public static string Get_CSS_RTM(string sInpunt)
{
string sOut = "";
char[] c = System.Environment.NewLine.ToCharArray();
string[] sRows = new string[] { };
string[] sRow = new string[] { };
bool Esc1 = false;
bool Esc2 = false;
bool Esc3 = false;
sRows = Regex.Split(sInpunt, "\t");
for (int i = 0; i 0)
{
if (sRows[i].IndexOf(Convert.ToChar("-")) < 0)
{
sRows[i] = sRows[i].Trim();
}
}
sRow = SplitChar(sRows[i]);
for (int j = 0; j < sRow.Length; j++)
{
if (sRow[j] == "/")
{
if (j < sRow.Length - 1)
{
if (sRow[j + 1] == "*")
{
Esc1 = true;
}
}
}
if (sRow[j] == "*")
{
if (j < sRow.Length - 1)
{
if (sRow[j + 1] == "/")
{
Esc1 = false;
j = j + 2;
}
}
}
if (j < sRow.Length)
{
char[] c2 = sRow[j].ToCharArray();
if ((c[0] == c2[0]) || (c[1] == c2[0]))
Esc2 = true;
else
Esc2 = false;
}
if (j < sRow.Length)
{
char[] c2 = sRow[j].ToCharArray();
if (c2[0] == Convert.ToChar(" "))
{
if ((sRow[j - 1] == ":") || (sRow[j - 1] == ";"))
Esc3 = true;
else
Esc3 = false;
if ((j + 1 < sRow.Length) && (!Esc3))
{
if ((sRow[j + 1] == ":") || (sRow[j + 1] == ";"))
Esc3 = true;
else
Esc3 = false;
}
}
else
{
Esc3 = false;
}
}
if ((!Esc1) && (!Esc2) && (!Esc3))
{
if (j <= sRow.Length - 1)
{
sOut += sRow[j];
}
}
}
return sOut;
}
private static string[] SplitChar(string sIn)
{
string[] sInSpl;
sInSpl = new string[sIn.Length];
for (int i = 0; i < sIn.Length; i++)
{
sInSpl[i] = sIn.Substring(i, 1);
}
return sInSpl;
}
}
Hi All, here an example on how loop on some rows in SQL Stored
Procedure, like a visual basic RecordSet or .NET Framework DataReader, simple put your query at line 7 of script:
SET @SQL ='SELECT Field1,Field2 FROM Table_Name '
That's all, We have spent lot of time to
find an example which works, finally it comes:
--LWebCode SQL Script
DECLARE @SQL NVARCHAR (4000)
DECLARE @DynamicSQL NVARCHAR(250)
DECLARE @Field1 NVARCHAR(100)
DECLARE @Field2 NVARCHAR(100)
DECLARE @outputCursor CURSOR
SET @SQL ='SELECT Field1,Field2 FROM Table_Name '
SET @DynamicSQL = 'SET @outputCursor = CURSOR FORWARD_ONLY STATIC FOR ' +
@SQL + ' ; OPEN @outputCursor'
exec sp_executesql -- sp_executesql will essentially create a sproc
@DynamicSQL, -- The SQL statement to execute (body of sproc)
N'@outputCursor CURSOR OUTPUT', -- The parameter list for the sproc: OUTPUT CURSOR
@outputCursor OUTPUT
FETCH NEXT FROM @outputCursor INTO @Field1,@IDField2
WHILE @@FETCH_STATUS = 0
BEGIN
/*here is loop put your code here*/
SET @SQL='SELECT * FROM
t_name WHERE Field1=''' + @Field1 + ''''
EXEC SP_EXECUTESQL @SQL
FETCH NEXT FROM @outputCursor INTO @Field1,@Field2
END
Hi All, Here LWebCode's class to directly run SQL statements whitout declare and initialize Connection and Command objects.
The class works for aspx page and also for Windows Forms,
in windows forms remember to change initial parameters like connection string, CodeID ect..
Very useful to write less code
Optionally in 2 ArrayList we can pass parameters and relative values
Class usage Example:
//Put this at top of your *.cs file in aspx page, or Windows Form Class
using LWebCode;
//simple calling function
public void Calls_iSQL_Class()
{
string cnString = ConfigurationManager.ConnectionStrings[0].ConnectionString;
string sCodeID = Request.QueryString["qryPar1"].ToString();
string sDate = Request.QueryString["qryPar2"].ToString();
ArrayList aFields = new ArrayList();
ArrayList aValues = new ArrayList();
aFields.Add("@CODEID");
aFields.Add("@DATE");
aValues.Add(sCodeID);
aValues.Add(sDate);
//Performing SQL INSERT and UPDATE Statement:
//Call Example with Parameters more secure
int res = iSQL.ExecuteNonQuery(cnString , "INSERT INTO MY_TABLE (CODE,DATE_R) VALUES (@CODEID,@DATE)",aFields ,aValues);
//Call Example without Parameters less secure don't use it, or use it carrefully recomended use for debugging SQL statement
int res2 = iSQL.ExecuteNonQuery(cnString , "UPDATE MY_TABLE SET DATE_R=NULL WHERE CODE=1235LWEBCODE", null , null);
//Performing Fast Reading Value valid only for 1 record with SQL SELECT Statemet
string sres = iSQL.ExecuteReader(cnString , "SELECT TOP 1 DATE_R FROM MY_TABLE WHERE CODE=1235LWEBCODE", null , null);
}
HERE THE LWebCode's CLASS:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
///
/// Class to execute sql statement like SELECT,UPDATE,INSERT other
/// whitout initialize connection and command
///
namespace LWebCode
{
public class iSQL
{
public iSQL()
{
}
///
/// Esegue un ExecuteNonQuery con un Singolo Parametro
///
/// The Connection Strings
/// Sql Statements to execute
/// Field Add '@'
/// Value
///
public static int ExecuteNonQuery(string CnString,string SQL,string Campo,string Valore)
{
cn = new SqlConnection(CnString);
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.Parameters.AddWithValue(Campo, Valore);
cmd.CommandText = SQL;
int ris = cmd.ExecuteNonQuery();
cn.Close();
cn.Dispose();
cmd.Dispose();
return ris;
}
///
/// Execute ExecuteScalar with multiple Parameters
///
/// The Connection Strings
/// Sql Statements to execute
/// Fields Array with @
/// Values Array
///
public static int ExecuteNonQuery(string CnString, string SQL, ArrayList Fields ,ArrayList Values)
{
SqlConnection cn = new SqlConnection(CnString);
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
for (int i = 0; i < Fields.Count; i++)
{
cmd.Parameters.AddWithValue(Fields[i].ToString(), Values[i]);
}
cmd.CommandText = SQL;
int ris=cmd.ExecuteNonQuery();
cn.Close();
cn.Dispose();
cmd.Dispose();
return ris;
}
///
/// Execute ExecuteScalar with multiple Parameters
///
/// The Connection Strings
/// Sql Statements to execute
/// Fields Array
/// Values Array
///
public static int ExecuteScalar(string CnString, string SQL, ArrayList Fields, ArrayList Values)
{
SqlConnection cn = new SqlConnection(CnString);
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
for (int i = 0; i < Fields.Count; i++)
{
cmd.Parameters.AddWithValue(Fields[i].ToString(), Values[i]);
}
cmd.CommandText = SQL;
int ris = Convert.ToInt32(cmd.ExecuteScalar());
cn.Close();
cn.Dispose();
cmd.Dispose();
return ris;
}
///
/// Execute Reader which return only 1 value
///
/// The Connection Strings
/// Sql Statements to execute
/// Fields Array
/// Values Array
///
public static string ExecuteReader(string CnString, string SQL, ArrayList Fields, ArrayList Values)
{
SqlConnection cn = new SqlConnection(CnString);
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = SQL;
for (int i = 0; i < Fields.Count; i++)
{
cmd.Parameters.AddWithValue(Fields[i].ToString(), Values[i]);
}
SqlDataReader rd;
rd = cmd.ExecuteReader();
string a = "";
while (rd.Read())
{
a = rd[0].ToString();
}
cmd.Dispose();
cn.Close();
rd.Close();
rd.Dispose();
return a;
}
}
}