useusing System;using System.Data;using System.Configuration;using System.Collections;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.Net;using System.IO;public partial class ScreenScrap : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){myWebText.Text = readHtmlPage("http://rprateek.blogspot.com");}private String readHtmlPage(string url){String result;WebResponse objResponse;WebRequest objRequest = System.Net.HttpWebRequest.Create(url);objResponse = objRequest.GetResponse();using (StreamReader sr =new StreamReader(objResponse.GetResponseStream())){result = sr.ReadToEnd();// Close and clean up the StreamReadersr.Close();}return result;}}
Monday, November 30, 2009
How to screen scrap data from Asp.Net application
useusing System;using System.Data;using System.Configuration;using System.Collections;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.Net;using System.IO;public partial class ScreenScrap : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){myWebText.Text = readHtmlPage("http://rprateek.blogspot.com");}private String readHtmlPage(string url){String result;WebResponse objResponse;WebRequest objRequest = System.Net.HttpWebRequest.Create(url);objResponse = objRequest.GetResponse();using (StreamReader sr =new StreamReader(objResponse.GetResponseStream())){result = sr.ReadToEnd();// Close and clean up the StreamReadersr.Close();}return result;}}
Thursday, November 26, 2009
How to find a string between two begin and end strings in C#
string myString = "<span>Senthil</span><span>Kumar</span>";
string[] result = GetStringInBetween("<span>",
"</span>", myString, true, true);
string next = result[1];
public static string[] GetStringInBetween(string strBegin,
string strEnd, string strSource,
bool includeBegin, bool includeEnd)
{
string[] result ={ "", "" };
int iIndexOfBegin = strSource.IndexOf(strBegin);
if (iIndexOfBegin != -1)
{
// include the Begin string if desired
if (includeBegin)
iIndexOfBegin -= strBegin.Length;
strSource = strSource.Substring(iIndexOfBegin + strBegin.Length);
int iEnd = strSource.IndexOf(strEnd);
if (iEnd != -1)
{
// include the End string if desired
if (includeEnd)
iEnd += strEnd.Length;
result[0] = strSource.Substring(0, iEnd);
// advance beyond this segment
if (iEnd + strEnd.Length < strSource.Length)
result[1] = strSource.Substring(iEnd + strEnd.Length);
}
}
else
// stay where we are
result[1] = strSource;
return result;
}
It's a handy utility that I've been using allot in some screen scrapping projects I've done lately.
Tuesday, November 24, 2009
NET SEND messages
closeFont();
Sunday, November 15, 2009
DiffGram XML Format
DiffGrams and DataSet
There are occasions when you want to compare the original data with the current data to get the changes made to the original data. One of the common example is saving data on Web Forms applications. When working with Web based data driven applications, you read data using a DataSet, make some changes to the data and sends data back to the database to save final data. Sending entire DataSet may be a costly affair specially when there are thousands of records in a DataSet. In this scenario, the best practice is to find out the updated rows of a DataSet and send only updated rows back to the database instead of the entire DataSet. This is where the DiffGrams are useful.
Note: Do you remember GetChanges method of DataSet? This method returns the rows that have been modified in the current version in a form of DataSet. This is how a DataSet knows the modified rows.
A DiffGram is an XML format that is used to identify current and original versions of data elements. Since the DataSet uses XML format to store and transfer data, it also use DiffGrams to keep track of the original data and the current data. When a DataSet is written as a DiffGram, not only a DiffGram stores original and current data, it also stores row versions, error information, and their orders.
DiffGram XML Format
The XML format for a DiffGram has three parts - data instance, diffgram before and diffgram errors. The <DataInstance> tag represents the data instance part of a diffgram, which represents the current data. The diffgram before is represented by the <diffgr:before> tag, which represents the original version of the data. The <diffgr:errors> tag represents the diffgram errors part, which stores the errors and related information. The diffgram itself is represented by tag <diffgr:diffgram>. The XML listed in Listing 1 represents the skeleton of a DiffGram..
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DataInstance>
</DataInstance>
<diffgr:before>
</diffgr:before>
<diffgr:errors>
</diffgr:errors>
</diffgr:diffgram>
Listing 1. A DiffGram format
Besides above discussed three sections, a DiffGram uses other elements. These are described in Table 1.
Table 1 describes the DiffGram elements that are defined in the DiffGram namespace urn:schemas-microsoft-com:xml-diffgram-v1.
- Id
<Customers diffgr:id="Customers1">.
- Parented
- hasChanges
- hasErrors
- Error
Now let's see an example of DiffGrams. The code listed in Listing 1 reads data from Employees tables and write in an XML document in DiffGram format.
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb"
Dim sql As String = "SELECT EmployeeID, FirstName, LastName, Title FROM Employees"
Dim conn As OleDbConnection = Nothing
Dim ds As DataSet = Nothing
' Create and open connection
conn = New OleDbConnection(connectionString)
If conn.State <> ConnectionState.Open Then
conn.Open()
End If
' Create a data adapter
Dim adapter As New OleDbDataAdapter(sql, conn)
' Create and fill a DataSet
ds = New DataSet("TempDtSet")
adapter.Fill(ds, "DtSet")
' Write XML in DiffGram format
ds.WriteXml("DiffGramFile.xml", XmlWriteMode.DiffGram)
' Close connection
If conn.State = ConnectionState.Open Then
conn.Close()
End If
Dim ds As New DataSet
' Fill with the data
ds.ReadXml("DiffGramFile.xml", XmlReadMode.DiffGram)
The GetChanges method of DataSet can be used to retrieve the rows that have been modified since the last time DataSet was filled, saved or updated. The GetChanges method returns a DataSet objects with modified rows.
Table 2 describes the DataRowState members.
- Added
- Deleted
- Detached
- Modified
- Unchanged
Dim tempDs As DataSet
tempDs = ds.GetChanges(DataRowState.Modified)
Now you can use new DataSet to bind it to data-bound controls or send back to the database to store results.