Wednesday, April 27, 2011

Serialize Code example

using System;
using System.IO;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Serialization;
using CurrencyConvertor;
using System.Web.Services;
using SerializeObj;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
WebClass myClass = new WebClass();
TextBox1.Text = myClass.name = "Sheo Narayan";
TextBox2.Text = myClass.address = "Washington Dc,US";

string xmlObject = SerializeAnObject(myClass);
TextBox3.Text = xmlObject.Insert(5, "hello");
//DropDownList1.SelectedItem.Value = CurrencyConvertor.Currency.ZWD.ToString();
TextBox4.Text = CurrencyConvertor.Currency.ZMK.ToString();
WebClass deSerializedClass = (WebClass)DeSerializeAnObject(xmlObject);

string name = deSerializedClass.name;
string address = deSerializedClass.address;
TextBox6.Text = name;
TextBox7.Text = address;

}
///

/// Serialize an object

///


///
///

private string SerializeAnObject(object obj)
{

System.Xml.XmlDocument doc = new XmlDocument();

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

System.IO.MemoryStream stream = new System.IO.MemoryStream();

try
{

serializer.Serialize(stream, obj);

stream.Position = 0;

doc.Load(stream);

return doc.InnerXml;

}

catch
{

throw;

}

finally
{

stream.Close();

stream.Dispose();

}

}

///

/// DeSerialize an object

///


///
///

private object DeSerializeAnObject(string xmlOfAnObject)
{

WebClass myObject = new WebClass();

System.IO.StringReader read = new StringReader(xmlOfAnObject);

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(myObject.GetType());

System.Xml.XmlReader reader = new XmlTextReader(read);

try
{

myObject = (WebClass)serializer.Deserialize(reader);

return myObject;

}

//catch
//{

// throw;

//}

finally
{

reader.Close();

read.Close();

read.Dispose();

}

}

//protected void Button2_Click(object sender, EventArgs e)
//{

// WebClass deSerializedClass = (WebClass)DeSerializeAnObject();

// string name = deSerializedClass.name;
// string address = deSerializedClass.address;
//}
//protected void Button2_Click(object sender, EventArgs e)
//{
// WebClass myClassObject =new WebClass();




//}
protected void Button2_Click(object sender, EventArgs e)
{
WebClass Class = new WebClass();
string df;

df = Class.name.ToString();


Service gk = new Service();
TextBox8.Text = gk.SerializeAnObject(df = TextBox9.Text);

object dl = new object();

dl = DeSerializeAnObject(TextBox8.Text);
TextBox10.Text = dl.ToString();

//string name = deSerializedClass.name;
//string address = deSerializedClass.address;
//TextBox9.Text = name;
//TextBox10.Text = address;



}
public class Book
{
public string Title;
public string Isbn;
public string fbi;
}
//deserialize

protected void Button3_Click1(object sender, EventArgs e)
{
Book b = new Book();
b.Title = "Windows Forms";
b.Isbn = "728372837";
b.fbi = "521";

XmlSerializer serializer = new XmlSerializer(typeof(Book));
TextWriter tw = new StreamWriter(Server.MapPath("Book1.xml"));
serializer.Serialize(tw, b);
tw.Close();
}
protected void Button4_Click1(object sender, EventArgs e)
{
XmlSerializer serializer = new XmlSerializer(typeof(Book));
TextReader tr = new StreamReader(Server.MapPath("Book1.xml"));
Book b = (Book)serializer.Deserialize(tr);
tr.Close();

Title.Text = b.Title;
Isbn.Text = b.Isbn;

}


protected void Button5_Click(object sender, EventArgs e)
{



XmlSerializer serializer = new XmlSerializer(typeof(Book));
TextReader tr = new StreamReader(Server.MapPath("book1.xml"));
Book b = (Book)serializer.Deserialize(tr);
tr.Close();
TextBox10.Text = tr.ToString();
Title.Text = b.Title;
Isbn.Text = b.Isbn;
}
[Serializable]
public class myTestClass
{
public string myString = "Hello World";
public int myInt = 1234;
public string[] myArray = new string[4];
private int myPrivateInt = 4321;

public string myMethod()
{
return "Hello World";
}
}
}

imp. links for .net tutorials

http://www.beansoftware.com/NET-Tutorials/Serialization-In-NET.aspx
http://www.eggheadcafe.com/articles/20040721.asp

http://www.webservicex.net/ws/wsdetails.aspx?wsid=10
http://www.dotnetfunda.com/articles/article98.aspx
http://www.codeguru.com/Csharp/Csharp/cs_webservices/tutorials/article.php/c5477
http://www.codeproject.com/KB/IP/httpwebrequest_response.aspx
http://www.dreamincode.net/forums/topic/48954-working-with-xml-files-in-c%23/#/
http://msdn.microsoft.com/en-us/library/ekw4dh3f.aspx
Read xml through Dataset.
http://www.dotnetspider.com/resources/35491-Get-xml-value-from-XML-file.aspx
http://www.dotnetjohn.com/articles.aspx?articleid=173
http://www.diranieh.com/NETSerialization/XMLSerialization.htm
C:\Dev\TestWebservice\Sample2\WebService3
http://www.codeguru.com/csharp/csharp/cs_webservices/tutorials/article.php/c12009__2/Using-SOAP-Headers-with-ASPNET-20-Web-Services.htm
http://www.codedigest.com/Articles/ASPNET/377_Reading_and_Writing_XML_files_in_C__

What are channels in .NET Remoting?

Channels represent the objects that transfer the other serialized objects from one application domain to another and from one computer to another, as well as one process to another on the same box. A channel must exist before an object can be transferred.

What are remotable objects in .NET Remoting?

Remotable objects are the objects that can be marshaled across the application domains. You can marshal by value, where a deep copy of the object is created and then passed to the receiver. You can also marshal by reference, where just a reference to an existing object is passed.

What’s a proxy of the server object in .NET Remoting?

It’s a fake copy of the server object that resides on the client side and behaves as if it was the server. It handles the communication between real server object and the client object. This process is also known as marshaling.

What are the consideration in deciding to use .NET Remoting or ASP.NET Web Services?

Remoting is a more efficient communication exchange when you can control both ends of the application involved in the communication process. Web Services provide an open-protocol-based exchange of informaion. Web Services are best when you need to communicate with an external organization or another (non-.NET) technology.

What is .net remoting?

.Net remoting is replacement of DCOM.
Using .Net remoting you can make remote object calls which lie in different application domains.
As the remote objects run in different process client calling the remote object can not call it
directly. So the client uses A proxy which looks like A real objects.
These method calls are called as “messages”.
Massages are serialized using “formatter ”class and sent to client “channel”.
Client channel communicates with server channel. Server channel uses as formatter to deserialize the massage and sends to the remote object.

Generics

Generics
When you design classes, you do so without a crystal ball. Classes are designed based on the available knowledge of who the consumers of the class likely could be, and the data types those developers would like to use with your class.
While developing a system, you may have a need to develop a class that stores a number of items of the integer type. To meet the requirement, you write a class named Stack that provides the capability to store and access a series of integers:
using System;

public class Stack
{
int[] items;

public void Push (int item) {...};
public int Pop() {...};

}

Suppose that at some point in the future, there is a request for this same functionality, only the business case requires the support for the double type. This current class will not support that functionality, and some changes or additions need to be made.
One option would be to make another class, one that provided support for the double type:
public class DoubleStack
{
double[] items;

public void Push (double item) {...};
public int Pop() {...};

}

But doing that is not ideal. It leaves you with two classes, and the need to create others if requests to support different types arise in the future.
The best case would be to have a single class that was much more flexible. In the .NET 1.x world, if you wanted to provide this flexibility, you did so using the object type:
using System;

public class Stack
{
object[] items;

public void Push (object item) {...};
public int Pop() {...};

}

This approach provides flexibility, but that flexibility comes at a cost. To pop an item off the stack, you needed to use typecasting, which resulted in a performance penalty:
double d = (double)stack.Pop;

In addition, that class allowed the capability to write and compile the code that added items of multiple types, not just those of the double type.
Fortunately, with version 2.0 of the .NET Framework, Generics provide us a more palatable solution. So what are Generics? Generics are code templates that allow developers to create type-safe code without referring to specific data types.
The following code shows a new generic, GenericStack. Note that we use a placeholder of M in lieu of a specific type:
public class GenericStack
{
M[] items;

void Push(M input) { }
public M Pop() {}
}

The following code shows the use of GenericStack. Here, two instances of the class are created, stack1 and stack2. stack1 can contain integers, whereas stack2 can contain doubles. Note that in place of M, the specific type is provided when defining the variables:
class TestGenericStack
{

static void Main()
{
// Declare a stack of type int
GenericStack stack1 = new GenericStack();

// Declare a list of type double
GenericStack stack2 = new GenericStack();

}
}

This provides support for us to use the functionality for the types we know we must support today, plus the capability to support types we'll be interested in in the future. If in the future, you create a class named NewClass, you could use that with a Generic with the following code:
// Declare a list of the new type
GenericStack list3 = new GenericStack();

In GenericStack class, M is what is called a generic-type parameter. The type specified when instantiating the GenericStack in code is referred to as a type argument. In the code for the TestGenericStack class, int and double, are both examples of type arguments.
In the preceding example, there was only one generic-type parameter, M. This was by choice and not because of a limitation in the .NET Framework. The use of multiple generic-type parameters is valid and supported, as can be seen in this example:
class Stock
{

X identifier;
Y numberOfShares;


...

}

Here are two examples of how this could then be used in code. In the first instance, the identifier is an int, and the number of shares a double. In the second case, the identifier is a string, and the number of shares is an int:
Stock x = new Stock();
Stock y = new Stock();

In these examples, we've also used a single letter to represent our generic-type parameter. Again, this is by choice and not by requirement. In the Stock class example, X and Y could have also been IDENTIFIER and NUMBEROFSHARES.
Now that you've learned the basics of generics, you may have a few questions: What if I don't want to allow just any type to be passed in, and want to make sure that only types deriving from a certain interface are used? What if I need to ensure that the type argument provided refers to a type with a public constructor? Can I use generic types in method definitions?
The good news is that the answer to each of those questions is yes.
There will undoubtedly be instances in which you will want to restrict the type arguments that a developer may use with the class. If, in the case of the Stock generic, you wanted to ensure that the type parameter provided for X was derived from IStockIdentifier, you could specify this, as shown here:
public class Stock where X:IStockIdentifier

Although this example uses a single constraint that X must derive from IStockIdentifier, additional constraints can be specified for X, as well as Y.
In regard to other constraints for X, you may want to ensure that the type provided has a public default constructor. In that way if your class has a variable of type X that creates a new instance of it in code, it will be supported.
By defining the class as
public class Stock where X:new()

you ensure that you can create a new instance of X within the class:
X identifier = new X();

As mentioned earlier, you can specify multiple constraints. The following is an example of how to specify that the Stock class is to implement both of the constraints listed previously:
public class Stock where X:IStockIdentifier, new()

In the GenericStack example, you saw that there were Push and Pop methods:
public class GenericStack
{
M[] items;

void Push(M input) { }
public M Pop() {}
}

Push and Pop are examples of generic methods. In that code, the generic methods are inside of generic types. It is important to note that they can also be used outside of generic types, as seen here:
public class Widget
{
public void WidgetAction(M m)
{...}
}

Note
It should be noted that attempts to cast a type parameter to the type of a specific class will not compile. This is in keeping with the purpose of genericscasting to a specific type of class would imply that the generic-type paremeter is, in fact, not generic. You may cast a type parameter, but only to an interface.

Inheritance is the last area we'll cover on Generics in this chapter. Inheritance is handled in a very straightforward mannerwhen defining a new class that inherits from a generic, you must specify the type argument(s).
If we were to create a class that inherited from our Stock class, it would resemble the following code:
public class Stock
{...}
public class MyStock : Stock
{...}

What is wcf?


The Windows Communication Foundation is meant to work well in any circumstance in which software entities must be made to communicate with one another. In fact, it is meant to always be the very best option: It provides performance that is about as good as, if not better than, any other alternative; it offers at least as many features and probably several more; and it will certainly always be the easiest solution to program