Misc Examples Source Code Files
== Calendar == WebForm1.aspx =================================================
<!-- Calendar Example
Show a web form with Calendar and LinkButton controls. -->
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="Calendar.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Calendar Example</title>
<link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Calendar Example</h2>
<form id="form1" runat="server">
<div>
<asp:Calendar ID="Calendar1" runat="server" BackColor="White"
BorderColor="#3366CC" BorderWidth="1px" CellPadding="1"
DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#003399" Height="200px" Width="220px">
<DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
<NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
<OtherMonthDayStyle ForeColor="#999999" />
<SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
<TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px"
Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
<TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
<WeekendDayStyle BackColor="#CCCCFF" />
</asp:Calendar>
<br />
<asp:TextBox ID="TextBox1" runat="server" CssClass="ctrl"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" CssClass="ctrl" OnClick="Button1_Click"
Text="Show Selected Date" />
<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/WebForm2.aspx">
Go to WebForm2.aspx</asp:LinkButton>
</div>
</form>
</body>
</html>
-- Calendar -- WebForm1.aspx.cs ----------------------------------------------
// Calendar Example
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Calendar
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = Calendar1.SelectedDate.ToString("D");
}
}
}
-- Calendar -- WebForm2.aspx -------------------------------------------------
<!-- Calendar Example
Show web forms that use the Calendar and LinkButton web controls -->
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"
Inherits="Calendar.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Calendar Example</title>
<link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Calendar Example</h2>
<p>This is WebForm2.aspx</p>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/WebForm1.aspx">
Go to WebForm1.aspx</asp:LinkButton>
</div>
</form>
</body>
</html>
-- Calendar -- Styles.css ----------------------------------------------------
/* Calendar Example */
body
{
font-family: Verdana;
font-size: 130%;
background-color: beige;
color: navy;
}
h2
{
color: maroon;
}
.ctrl
{
width: 400px;
font-size: 100%;
}
== CurrRateFromWeb == Program.cs ============================================
// Get exchange rate from Internet, use it to compute target amount from
source rate, target rate, and source amount.
using System;
using System.IO;
using System.Net;
namespace CurrRateFromWeb
{
class Program
{
public static void Main()
{
string prefix = "http://download.finance.yahoo.com/d/quotes.csv?s=";
string suffix = "=X&f=sl1d1t1ba&e=.csv";
string sourceCurrency = "GBP";
string targetCurrency = "USD";
string url = prefix + sourceCurrency + targetCurrency + suffix;
Console.WriteLine(GetCurrencyRate(url, sourceCurrency, targetCurrency));
Console.ReadLine();
}
public static string GetCurrencyRate(string url, string source, string target)
{
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string rate = "";
using (StreamReader sr = new StreamReader(data))
{
string line = sr.ReadLine();
string[] fields = line.Split(',');
rate = fields[1];
}
return rate;
}
}
}
== VehicleWebPage == WebForm1.aspx ==========================================
<!-- VehicleWebPage Example
Store Vehicle objects populated with data from a
web page. Then write all of the objects out to a file. -->
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="VehicleWebPage.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>VehicleWebPage Example</title>
<link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>VehicleWebPage Example</h2>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtModel" runat="server" CssClass="ctrl"></asp:TextBox>
<br />
Model<br />
<br />
<asp:Button ID="txtAdd" runat="server" OnClick="txtAdd_Click"
Text="Add Vehicle to Collection" CssClass="ctrl" />
<br />
<br />
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click"
Text="Save to Disk" CssClass="ctrl" />
</div>
</form>
</body>
</html>
-- VehicleWebPage -- WebForm1.aspx.cs ---------------------------------------
// VehicleWebPage Example
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace VehicleWebPage
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["col"] = new ArrayList();
}
}
protected void txtAdd_Click(object sender, EventArgs e)
{
ArrayList col = (ArrayList)Session["col"];
Vehicle v = new Vehicle(txtModel.Text);
col.Add(v);
Session["col"] = col;
}
protected void btnSave_Click(object sender, EventArgs e)
{
StreamWriter sr = new StreamWriter(
"c:/jost/vehicle.txt");
ArrayList col = (ArrayList)Session["col"];
foreach (Vehicle v in col)
{
sr.WriteLine(v);
}
sr.Close();
}
}
}
-- VehicleWebPage == Styles.css ---------------------------------------------
/* VehicleWebPage Example */
body
{
font-family: Verdana;
font-size: 130%;
background-color: beige;
color: navy;
}
h2
{
color: maroon;
}
.ctrl
{
width: 300px;
font-size: 100%;
}
== EmployeeWebPage == WebForm1.aspx =========================================
<!-- EmployeeWebPage Example
Display Person, Employee, or Executive items on the web page
The btnAddPerson_Click, btnAddPerson_Click, and btnAddPerson_Click
methods allows new items to be added to the Dictionary collection. -->
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="EmployeeWebPage.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>EmployeeWebPage Example</title>
<link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>EmployeeWebPage Example</h2>
<form id="form1" runat="server">
<div id="textboxes">
<asp:TextBox ID="txtName" runat="server" CssClass="ctrl" /> Name<br />
<br />
<asp:TextBox ID="txtGender" runat="server" CssClass="ctrl" /> Gender<br />
<br />
<asp:TextBox ID="txtAge" runat="server" CssClass="ctrl" /> Age<br />
<br />
<asp:TextBox ID="txtID" runat="server" CssClass="ctrl" /> ID<br />
<br />
<asp:TextBox ID="txtSalary" runat="server" CssClass="ctrl" /> Salary<br />
<br />
<asp:TextBox ID="txtBonus" runat="server" CssClass="ctrl" /> Bonus<br />
<br />
</div>
<div id="other">
<asp:DropDownList ID="ddlKeys" CssClass="ctrl" runat="server">
<asp:ListItem>Select an item.</asp:ListItem>
</asp:DropDownList><br />
<br />
<asp:Button ID="btnShow" runat="server" Text="Show Selected Item"
CssClass="ctrl" OnClick="btnShow_Click" /><br />
<br />
<asp:Button ID="btnAddPerson" runat="server" Text="Add New Person"
CssClass="ctrl" OnClick="btnAddPerson_Click" /><br />
<br />
<asp:Button ID="btnAddEmployee" runat="server" Text="Add New Employee"
CssClass="ctrl" OnClick="btnAddEmployee_Click"
/><br />
<br />
<asp:Button ID="btnAddExecutive" runat="server" Text="Add New Executive"
CssClass="ctrl" OnClick="btnAddExecutive_Click"
/><br />
<br />
</div>
</form>
</body>
</html>
-- VehicleWebPage -- WebForm1.aspx.cs ---------------------------------------
// EmployeeWebPage Example
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace EmployeeWebPage
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person p;
string key;
if(!IsPostBack)
{
Dictionary<string, Person> col = new Dictionary<string, Person>();
p = new Person("Nancy", 'F', 23);
key = p.Name;
col.Add(key, p);
ddlKeys.Items.Add(key);
p = new Employee("Larry", 'M', 35, 3454, 45000.00);
key = p.Name;
col.Add(key, p);
ddlKeys.Items.Add(key);
p = new Executive("Rita", 'F', 41, 8727, 80000.00, 25000.00);
key = p.Name;
col.Add(key, p);
ddlKeys.Items.Add(key);
Session["col"] = col;
}
}
protected void btnShow_Click(object sender, EventArgs e)
{
string key;
Person p;
Employee emp;
Executive exec;
Dictionary<string, Person> col = (Dictionary<string, Person>)Session["col"];
if (ddlKeys.SelectedIndex == 0)
{
// Do nothing.
}
else
{
key = ddlKeys.SelectedItem.ToString();
p = col[key];
switch (p.GetType().Name)
{
case "Person":
txtName.Text = p.Name;
txtGender.Text = p.Gender.ToString();
txtAge.Text = p.Age.ToString();
txtID.Text = "";
txtSalary.Text = "";
txtBonus.Text = "";
break;
case "Employee":
emp = (Employee)p;
txtName.Text = emp.Name;
txtGender.Text = emp.Gender.ToString();
txtAge.Text = emp.Age.ToString();
txtID.Text = emp.ID.ToString();
txtSalary.Text = emp.Salary.ToString();
txtBonus.Text = "";
break;
case "Executive":
exec = (Executive) p;
txtName.Text = exec.Name;
txtGender.Text = exec.Gender.ToString();
txtAge.Text = exec.Age.ToString();
txtID.Text = exec.ID.ToString();
txtSalary.Text = exec.Salary.ToString();
txtBonus.Text = exec.Bonus.ToString();
break;
}
}
}
protected void btnAddPerson_Click(object sender, EventArgs e)
{
Dictionary<string, Person> col =
(Dictionary<string, Person>)Session["col"];
Person p = new Person( );
p.Name = txtName.Text;
p.Gender = char.Parse(txtGender.Text);
p.Age = int.Parse(txtAge.Text);
string key = p.Name;
col.Add(key, p);
ddlKeys.Items.Add(key);
Session["col"] = col;
}
protected void btnAddEmployee_Click(object sender, EventArgs e)
{
Dictionary<string, Person> col =
(Dictionary<string, Person>)Session["col"];
Employee emp = new Employee();
emp.Name = txtName.Text;
emp.Gender = char.Parse(txtGender.Text);
emp.Age = int.Parse(txtAge.Text);
emp.ID = int.Parse(txtID.Text);
emp.Salary = double.Parse(txtSalary.Text);
string key = emp.Name;
col.Add(key, emp);
ddlKeys.Items.Add(key);
Session["col"] = col;
}
protected void btnAddExecutive_Click(object sender, EventArgs e)
{
Dictionary<string, Person> col =
(Dictionary<string, Person>)Session["col"];
Executive exec = new Executive();
exec.Name = txtName.Text;
exec.Gender = char.Parse(txtGender.Text);
exec.Age = int.Parse(txtAge.Text);
exec.ID = int.Parse(txtID.Text);
exec.Salary = double.Parse(txtSalary.Text);
exec.Bonus = double.Parse(txtBonus.Text);
string key = exec.Name;
col.Add(key, exec);
ddlKeys.Items.Add(key);
Session["col"] = col;
}
}
}
-- EmployeePage -- Person ---------------------------------------------------
using System;
namespace EmployeeWebPage
{
// EmployeeWebPage Example.
public class Person
{
protected string mvarName;
protected char mvarGender;
private int mvarAge;
public Person()
{
mvarName = "";
mvarGender = '\0';
mvarAge = 0;
}
public Person(string theName, char theGender, int theAge)
{
mvarName = theName;
mvarGender = theGender;
mvarAge = theAge;
}
public override string ToString()
{
return mvarName + " " + mvarGender + " " + mvarAge;
}
public string Name
{
get { return mvarName; }
set { mvarName = value; }
}
public char Gender
{
get { return mvarGender; }
set { mvarGender = value; }
}
public int Age
{
get { return mvarAge; }
set { mvarAge = value; }
}
}
}
-- VehicleWebPage -- Employee -----------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace EmployeeWebPage
{
/// EmployeeWebPage Example.
public class Employee : Person
{
protected int mvarID;
protected double mvarSalary;
public Employee() : base()
{
mvarID = 0;
mvarSalary = 0.00;
}
public Employee(string theName, char theGender,
int theAge, int theID, double theSalary) :
base(theName, theGender, theAge)
{
mvarID = theID;
mvarSalary = theSalary;
}
public override string ToString()
{
return base.ToString() + mvarID + " " + mvarSalary;
}
public int ID
{
get { return mvarID; }
set { mvarID = value; }
}
public double Salary
{
get { return mvarSalary; }
set { mvarSalary = value; }
}
}
}
-- VehicleWebPage -- Executive ----------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace EmployeeWebPage
{
/// EmployeeWebPage Example
public class Executive : Employee
{
private double mvarBonus;
public Executive() : base()
{
mvarBonus = 0.00;
}
public Executive(string theName, char theGender, int theAge,
int theID, double theSalary, double theBonus) :
base(theName, theGender, theAge, theID, theSalary)
{
mvarBonus = theBonus;
}
public override string ToString()
{
return base.ToString() + mvarBonus;
}
public double Bonus
{
get { return mvarBonus; }
set { mvarBonus = value; }
}
}
}
== BinaryFormatter == SerializeOut.cs =======================================
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace BF
{
/// <summary>
/// BinaryFormatter Example: Serialize a Hashtable object
/// disc using BinaryFormatter. Then deserialize the data
/// back to the Hashtable object.
/// </summary>
/// <remarks>
/// The class of each object contained in the Hashtable object
/// must be marked with the [Serializable] attribute.
/// </remarks>
public class SerializeOut
{
public static void Main(string[] args)
{
Hashtable col = new Hashtable();
string outputFile = "Employee.bin";
BinaryFormatter bf = new BinaryFormatter();
// Create and open filestream.
FileStream fs = File.Open(outputFile, FileMode.Create);
// Create one object of each class in the
// inheritance hierarchy.
col.Add("Scott", new Person("Scott", 'M', 40));
col.Add("Mary", new Employee("Mary", 'F', 40,
45393, 55000));
col.Add("Maggie", new Executive("Maggie", 'F',
28, 76293, 198000, 40000));
// Print out items in the hashtable using
// each objects ToString method.
foreach(DictionaryEntry d in col)
Console.WriteLine(d.Value);
// Save hashtable using binary serialization.
bf.Serialize(fs, col);
fs.Close();
Console.ReadLine();
}
}
}
-- BinaryFormatter -- SerializeIn.cs ----------------------------------------
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace BF
{
/// <summary>
/// BinaryFormatter Example: Serialize a Hashtable object
/// disc using BinaryFormatter. Then deserialize the data back
/// to the Hashtable object.
/// </summary>
/// <remarks>
/// The class of each object contained in the Hashtable object
/// must be marked with the [Serializable] attribute.
/// </remarks>
public class SerializeIn
{
public static void Main()
{
string inputFile = "Employee.bin";
Hashtable col = new Hashtable();
BinaryFormatter bf = new BinaryFormatter();
// Create and open file stream.
FileStream fs = File.Open(inputFile, FileMode.Open);
// Deserialize hashtable.
col = (Hashtable)bf.Deserialize(fs);
fs.Close();
// Print out hashtable collection.
// Note that items will print in an unpredictable order.
foreach(DictionaryEntry d in col)
Console.WriteLine(d.Value);
Console.WriteLine();
// Get Object from hashtable by key.
Console.WriteLine(col["Mary"]);
Console.ReadLine();
}
}
}
-- BinaryFormatter -- Person.cs ---------------------------------------------
using System;
namespace BF
{
/// <summary>
/// BinaryFormatter Example
/// </summary>
[Serializable]
public class Person
{
protected string name;
protected char gender;
protected int age;
// Needed for compatibility.
public Person() { }
public Person(string theName, char theGender, int theAge)
{
name = theName;
gender = theGender;
age = theAge;
}
public override string ToString()
{
return name + " " + gender + " " + age;
}
}
}
-- BinaryFormatter -- Employee.cs -------------------------------------------
using System;
namespace BF
{
/// <summary>
/// BinaryFormatter Example
/// </summary>
[Serializable]
public class Employee : Person
{
protected int id;
protected double salary;
// Needed for compatibility.
public Employee() { }
public Employee(string theName, char theGender, int theAge,
int theID, double theSalary) : base(theName, theGender, theAge)
{
id = theID;
salary = theSalary;
}
public override string ToString()
{
return base.ToString() + " " + id + " " + salary;
}
}
}
-- BinaryFormatter -- Executive.cs ------------------------------------------
using System;
namespace BF
{
/// <summary>
/// BinaryFormatter Example
/// </summary>
[Serializable]
public class Executive : Employee
{
private double bonus;
// Needed for compatibility.
public Executive() { }
public Executive(string theName, char theGender, int theAge,
int theID, double theSalary, double theBonus) :
base(theName, theGender, theAge, theID, theSalary)
{
bonus = theBonus;
}
public override string ToString()
{
return base.ToString() + " " + bonus;
}
}
}
-- EmployeeXlmSerializer -- SerializeOut.cs ---------------------------------
== EmployeeXlmSerializer == SerializeOut.cs =================================
using System;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
namespace EmployeeXmlSerializer
{
// Warning: Do not use an XmlSerializer object with a Hashtable
// or generic Dictionary collection. I know it works with
// an ArrayList collection. Test this example on a generic List
// collection before trying it for your BooksMusic project.
/// <summary>
/// EmployeeXmlSerializer Example: Serializes data from
/// an ArrayList collection to an Xml file and deserializes
/// it back to an ArrayList.
/// <remarks>
/// Unlike when using BinaryFormatter, the extra types
/// stored in the ArrayList collection must be specified
/// in the XmlSerializer constructor. The [Serializable]
/// attributes needed for BinaryFormatter are also not
/// required.
/// </summary>
/// </remarks>
public class SerializeOut
{
public static void Main()
{
ArrayList col = new ArrayList(); ;
// Create StreamWriter object.
StreamWriter sw = new StreamWriter("Employee.xml");
// Create XmlSerializer object.
Type[] extraTypes = new Type[] {typeof(Person),
typeof(Employee), typeof(Executive)};
XmlSerializer xmls = new XmlSerializer(typeof(ArrayList),
extraTypes);
// Create one object of each class in the
// inheritance hierarchy.
col.Add(new Person("Scott", 'M', 40));
col.Add(new Employee("Mary", 'F', 40, 45393, 55000));
col.Add(new Executive("Maggie", 'F', 28, 76293,
198000, 40000));
// Print out items in the collection using each
// object's ToString method.
foreach(Person p in col)
Console.WriteLine(p);
// Serialize ArrayList collection.
xmls.Serialize(sw, col);
// Close stream writer to flush buffer.
sw.Close();
Console.ReadLine();
}
}
}
-- EmployeeXlmSerializer -- SerializeIn.cs ----------------------------------
using System;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
namespace EmployeeXmlSerializer
{
/// <summary>
/// EmployeeXmlSerializer Example: Serializes data from an
/// ArrayList collection to an Xml file and deserializes
/// it back to an ArrayList.
/// <remarks>
/// Unlike when using BinaryFormatter, the extra types
/// stored in the ArrayList collection must be specified
/// in the XmlSerializer constructor. The [Serializable]
/// attributes needed for BinaryFormatter are also not
/// required.
/// </summary>
/// </remarks>
public class SerializeIn
{
public static void Main()
{
ArrayList col = new ArrayList(); ;
// Create StreamReader object.
StreamReader sr = new StreamReader("Employee.xml");
// Create XmlSerializer object.
Type[] extraTypes = new Type[] {typeof(Person),
typeof(Employee), typeof(Executive)};
XmlSerializer xmls = new XmlSerializer(typeof(ArrayList),
extraTypes);
// Deserialize hashtable
col = (ArrayList)xmls.Deserialize(sr);
sr.Close();
// Print out ArrayList collection.
foreach(Person p in col)
Console.WriteLine(p);
Console.WriteLine();
// Get Object from arraylist by index.
Console.WriteLine(col[1]);
Console.ReadLine();
}
}
}
-- EmployeeXlmSerializer -- Person.cs ---------------------------------------
using System;
namespace EmployeeXmlSerializer
{
/// <summary>
/// EmployeeXmlSerializer Example
/// </summary>
public class Person
{
public string name;
public char gender;
public int age;
// Needed for compatibility.
public Person() { }
public Person(string theName, char theGender, int theAge)
{
name = theName;
gender = theGender;
age = theAge;
}
public override string ToString()
{
return name + " " + gender + " " + age;
}
}
}
-- EmployeeXlmSerializer -- Employee.cs -------------------------------------
using System;
namespace EmployeeXmlSerializer
{
/// <summary>
/// EmployeeXmlSerializer Example
/// </summary>
public class Employee : Person
{
public int id;
public double salary;
// Needed for compatibility.
public Employee() { }
public Employee(string theName, char theGender, int theAge,
int theID, double theSalary) : base(theName, theGender, theAge)
{
id = theID;
salary = theSalary;
}
public override string ToString()
{
return base.ToString() + " " + id + " " + salary;
}
}
}
-- EmployeeXlmSerializer -- Executive.cs ------------------------------------
using System;
namespace EmployeeXmlSerializer
{
/// <summary>
/// EmployeeXmlSerializer Example
/// </summary>
public class Executive : Employee
{
public double bonus;
// Needed for compatibility.
public Executive() { }
public Executive(string theName, char theGender, int theAge,
int theID, double theSalary, double theBonus) :
base(theName, theGender, theAge, theID, theSalary)
{
bonus = theBonus;
}
public override string ToString()
{
return base.ToString() + " " + bonus;
}
}
}
== Chess == Program.cs ======================================================
// Chess Example
// Write the chess symbols for K, Q, R, B, N, P to a
// file in UTF-32 format. The BOM magic number for
// UTF-32 is feff. Read the file with NotePad in
// Unicode (Little Endian UTF-32) format.
using System;
using System.IO;
using System.Text;
namespace Chess
{
class Program
{
public static void Main()
{
using (StreamWriter sw = new StreamWriter("c:/jost/chess1.txt"))
{
sw.Write("\xff\xfe\x54\x26\x55\x26\x56\x26\x57\x26\x58\x26\x59\x26");
}
using (FileStream stream = File.Open("c:/jost/chess2.txt",
FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(stream, Encoding.Unicode))
{
sw.Write("\x2654\x2655\x2656\x2657\x2658\x2659");
}
}
Console.WriteLine("Files written to disk.");
Console.ReadLine();
}
}
}
== Seeds == WebForm1.aspx ===================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="Seeds.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Seeds Example</title>
<link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Seeds Example</h2>
<form id="form1" runat="server">
<div>
<asp:Literal ID="litPage" runat="server" />
</div>
</form>
</body>
</html>
-- Seeds -- WebForm1.aspx.cs ------------------------------------------------
using System;
using System.Xml;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// Seeds Example
// Use an XmlTextReader to read and XML file and use the data
// to construct an HTML page with tags in a Literal control.
namespace Seeds
{
public partial class WebForm1 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
XmlTextReader r = new XmlTextReader(Server.MapPath("seed-catalog.xml"));
while (r.Read()) {
switch (r.NodeType) {
case XmlNodeType.Element:
switch (r.Name)
{
case "CatalogItems":
litPage.Text += "<table>\n";
break;
case "CatalogItem":
litPage.Text += "<tr> ";
break;
case "ImageName":
litPage.Text += "<td><img src=\"";
break;
default:
litPage.Text += "<td>";
break;
}
break;
case XmlNodeType.Text:
litPage.Text += r.Value;
break;
case XmlNodeType.EndElement:
switch (r.Name)
{
case "CatalogItems":
litPage.Text += "</table>\n";
break;
case "CatalogItem":
litPage.Text += " </tr>\n";
break;
case "ImageName":
litPage.Text += "\" /></td>";
break;
default:
litPage.Text += "</td>";
break;
}
break;
}
}
}
}
}
-- Seeds -- Styles.css ------------------------------------------------------
/* EmployeeWebPage Example */
body
{
font-family: Verdana;
font-size: 130%;
background-color: beige;
color: navy;
}
h2
{
color: maroon;
}
table
{
border-collapse: collapse;
}
td
{
border-style: solid;
border-width: 1px;
padding: 5px;
}
-- Seeds -- seed-catalog.xml (Data File) ------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<CatalogItems>
<CatalogItem>
<IDNumber>1002</IDNumber>
<Description>Cucumber</Description>
<Price>2.56</Price>
<ImageName>cuc.jpg</ImageName>
</CatalogItem>
<CatalogItem>
<IDNumber>1006</IDNumber>
<Description>Lettuce</Description>
<Price>1.98</Price>
<ImageName>let.jpg</ImageName>
</CatalogItem>
<CatalogItem>
<IDNumber>1131</IDNumber>
<Description>Green Pepper</Description>
<Price>3.17</Price>
<ImageName>grp.jpg</ImageName>
</CatalogItem>
<CatalogItem>
<IDNumber>1271</IDNumber>
<Description>Sweet Corn</Description>
<Price>2.36</Price>
<ImageName>swc.jpg</ImageName>
</CatalogItem>
<CatalogItem>
<IDNumber>1334</IDNumber>
<Description>Watermelon</Description>
<Price>2.99</Price>
<ImageName>wtm.jpg</ImageName>
</CatalogItem>
</CatalogItems>
=============================================================================