Thursday, February 26, 2009

Silverlight: Web service handling

Full story here.

Silverlight: if your application doesn't run on the local server...

If you are having problems loading a Silverlight 2.0 application, and you have exhausted all options, you may need to register the XAP file extension in IIS. It is a simple fix. 

Open your IIS Manager, double-click on the MIME Types icon, and then on the Add... link on the right. 
Register the MIME type for .xap to application/x-silverlight-app.

Silverlight: cross domain policy

Click here for the full article.

Wednesday, February 18, 2009

ASP: Cookies - adding new values

For example:

   HttpCookie cookie = Response.Cookies["someId"];
   cookie.Values.Add("key_X", "value_X");


   // or, just:
   cookie.Values["key_Y"] = "value_Y";

ASP: Accessing GET and POST parameters from C#

To access only POST parameters:
   Request.Form["parmeter_name"];

To access only GET parameters:
   Request.QueryString["parameter_name"];

To access GET or POST parameters:
   Request["parameter_name"];

C#: Deserialising XML files

If, for example, you have a books.xml file like this:
<books xsi="....">
  <bookinfo>
    <isbn>0596009305</isbn>
    <author>Bill Bosan</author>
    <title>All About Bulbs</title>
    <price>5.99</price>
    <description>Just when you thought...</description>
  </bookinfo>
  ....
</books>
and you wanted to read it into a list of objects in your .CS file.

First, you need to create a BookInfo class. For example:

public class BookInfo {
private string _id;
private string _author;
private string _title;
private decimal _price;
private string _description;


public string Isbn {
get { return _id; }
set { _id = value; }
}
public string Author {

...
}
}

Then, in your main code you would do something like this:

private void ReadXml(string filename) {
List _catalog;
string fullPath = HttpContext.Current.Server.MapPath(filename);
if (File.Exists(fullPath) == false)
return;
XmlSerializer serializer = new XmlSerializer(typeof(List));
using (FileStream stream = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
_catalog = (List)serializer.Deserialize(stream);
}

}

Voila!

Friday, February 13, 2009

C#: Compressing, uncompressing, encoding...

To compress data and use Unicode character encoding:

void WriteCompressedDataToStream(string data, Stream destStm)
{
using (DeflateStream zipStm = new DeflateStream(destStm, CompressionMode.Compress))
using (StreamWriter destWriter = new StreamWriter(zipStm, Encoding.Unicode))
destWriter.Write(data);
}

To decompress the above:

string ReadCompressedDataFromStream(Stream srcStm)
{
using (DeflateStream zipStm = new DeflateStream(srcStm, CompressionMode.Decompress))
using (StreamReader srcRdr = new StreamReader(zipStm))
return srcRdr.ReadToEnd();
}

C#: Reading and Writing Full File Content (text or binary)

To read all text:

string fileName = "c:\dir\somefile.txt";
string text = File.ReadAllText(fileName);

To write all text:

string fileName = "c:\dir\somefile.txt";
string text = "Here's some text...\r\nAnd some more in the next line...";
File.WriteAllText(fileName, text);


To read all bytes:

byte[] buffer = new byte[BIG_ENOUGH_SIZE];
string fileName = "somefile.bin";
// opens a binary file, reads bytes, then closes
buffer = File.ReadAllBytes(fileName);

To write all bytes:

// creates a new file (or overwrites, writes bytes, then closes
File.WriteAllBytes(filename, buffer);