Wednesday, October 17, 2007

Writing data into XML files

You can use XmlTextWriter to write XML. XmlTextwriter is an implementation of the XmlWriter class. This class provides number of validatins to make sure xml is well formed. Here is the code to create a XML file.

using System.Xml;



XmlTextWriter myXmlTextWriter = new XmlTextWriter("student.xml", null);

myXmlTextWriter.Formatting = Formatting.Indented;
myXmlTextWriter.WriteStartDocument(false);
myXmlTextWriter.WriteDocType("students", null, "students.dtd", null);
myXmlTextWriter.WriteComment("This file represents a student in the institute");
myXmlTextWriter.WriteStartElement("students");
myXmlTextWriter.WriteStartElement("student", null);
myXmlTextWriter.WriteAttributeString("id","BCS00094");
myXmlTextWriter.WriteAttributeString("category", "Bsc");
myXmlTextWriter.WriteAttributeString("course", "Computer Science");
myXmlTextWriter.WriteElementString("birthdate", null, "1979-05-22");
myXmlTextWriter.WriteStartElement("Name", null);
myXmlTextWriter.WriteElementString("first-name", "Shawn");
myXmlTextWriter.WriteElementString("last-name", "Tait");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteElementString("GPA", "4.55");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteEndElement();

myXmlTextWriter.Flush();
myXmlTextWriter.Close();


WriteStartDocument(Boolean) - writes the xml declaration with the version 1.0 and the standalone attribute.
WriteComment - writes a comment containing the specified text.
WriteStartElement - writes the specified start tag.
WriteElementString - writes an element containing a string value.
WriteEndElement - writes the specified end tag.
WriteAttributeString - writes an attribute with a specified value.

DevDay 2007

Yesterday I've been to DevDay 2007 organized by Sri Lanka .Net forum held at Mount Lavinia hotel. There were 4 interesting technical sessions and later the party. All the 4 sessions were great. It was worth to attend.

Discover Next Generation Visual Studio and .NET 3.5
An Adventure with C# 3.0 and LINQ
By: Chua Wen Ching (MVP - C#)

WPF, a new way of looking @ your Windows Applications
Silverlight: The web just got richer
By: T.N.C. Venkata Rangan (Microsoft Regional Director)

Tuesday, October 02, 2007

Rugby World Cup 2007 - Knockout Stage

Rugby World Cup 2007 first round is over and 8 teams were selected to play the second stage. This will be a knockout out stage and unbeaten team in next 3 games will get the opportunity to lift the cup and crown as RWC 2007 champs.

As the tournament started with a upset by Argentina beating host France, Argentina managed to continue their rhythm and finish the first round as the top of their pool. They will be a strong contender in the knockout stage and it'll be a very interesting quarterfinal Argentina Vs Scotland.
Fiji managed to enter to the second round by beating Wales. So they'll meet South Africa in their first match in the second round.

Other 2 quarterfinals will be Australia Vs England and New Zealand Vs France. New Zealand finished all their first round matches very promisingly and they are the favorites in this tournament. But their lack of performances in the big games is always a huge disadvantage for them and also France is seeking for an opportunity to cover their humiliation defeat by Argentina in the first round, so they'll play their hearts out in front of their own crowd, in their own grounds. Nevertheless as I said earlier this is the best NZ team in a world cup after 1992 So they'll smell the cup as no one else does.

Creating a new XML file using XMLDocument

Here is the XML file:

<?xml
version="1.0" encoding="utf-8"?>
<Students>
  <Student>
    <id>1</
id>
    <name>Michael Jones</
name>
  </Student>
</Students>


Here is the code to generate the XML file:


using System.Xml;



XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

// Create the root element
XmlElement rootNode = xmlDoc.CreateElement(
"Students");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);

// Creating the parent node
XmlElement parentNode = xmlDoc.CreateElement(
"Student");
xmlDoc.DocumentElement.AppendChild(parentNode);

// Create the required nodes
XmlElement eleId = xmlDoc.CreateElement(
"id");
XmlElement eleName = xmlDoc.CreateElement(
"name");

// retrieve the text
XmlText textId = xmlDoc.CreateTextNode("1");
XmlText textName = xmlDoc.CreateTextNode("Michael Jones");

// append the nodes to the parentNode without the value
parentNode.AppendChild(eleId);
parentNode.AppendChild(eleName);

// save the value of the fields into the nodes
eleId.AppendChild(textId);
eleName.AppendChild(textName);

xmlDoc.Save(Request.MapPath() + "Students.xml");