/// method for reading an XML file into a DataTable
///</summary>
///<param name="file">name (and path) of the XML file</param>
///<returns></returns>
publicDataTable ReadXML(string file)
{
//create the DataTable that will hold the data
DataTable table = newDataTable("Employee"); //create the table with the appropriate column names
table.Columns.Add("EmpId", typeof(int));
table.Columns.Add("DeptId", typeof(int));
table.Columns.Add("TraderName", typeof(string));
table.Columns.Add("EmpName", typeof(string));
try
{
//open the file using a Stream
if (File.Exists(file))
{
using (Stream stream = newFileStream(file, FileMode.Open,
FileAccess.Read))
{
//use ReadXml to read the XML stream
table.ReadXml(stream);
//return the results
}
}
return table;
}
catch (Exception ex)
{
return table;
}
}
staticvoid Main(string[] args)
{
Program p=newProgram();
p.PrepareXMlDoc();
Console.ReadKey();
}
}
}
Note : the above scenario will work if and only if Exiting Xmlfile records and new record have the same structure, otherwise it will override with new record, and the existing records in the Xmlfile will be lost.
This article describes how to create a web quiz from the information in a Xml File and c#. A quiz is a great way to test your knowledge. An online quiz is a great addition to your web site that could keep your visitors glued for a few more minutes. I developed this application using c# and xml data. I have seen several examples like this but I found all applications are in vb.net. so, I have taken some suggestion from that articles and I developed this application.
Please take one Xml file Named as Quiz.xml.
XML Data
Data for the online quiz is kept in an XML document named quiz.xml A valid XML document consists of a root element called quiz, which has at least one element called mchoice (short for multiple-choice). Each mchoice element has one question child element, and two or more answer child elements. The answer element may have the correct attribute with possible value of either yes or no. In fact, you should supply the correct attribute with a value of yes to one of the answers in the same mchoice, otherwise there will be no correct answer for the question.
<question>C# (C-Sharp) was developed by</question>
<answer>Sun Microsystems</answer>
<answercorrect="yes">Microsoft</answer>
<answer>Intel</answer>
<answer>IBM</answer>
</mchoice>
<mchoice>
<question>What is C# (C-Sharp)?</question>
<answer>An Operating System </answer>
<answer>An Programming Tool </answer>
<answercorrect="yes">An Programming Language </answer>
<answer> None of these </answer>
</mchoice>
<mchoice>
<question>C# (C-Sharp) belong to which framework ?</question>
<answercorrect="yes">.NET</answer>
<answer>Java</answer>
<answer>It does't belong to any framework</answer>
<answer>None of these</answer>
</mchoice>
<mchoice>
<question>What is CLR ?</question>
<answercorrect="yes"> Common Language Runtime</answer>
<answer>Conventional Language Runtime</answer>
<answer>Clarified Language Runtime</answer>
<answer>Colloquial Language Runtime</answer>
</mchoice>
<mchoice>
<question>Object Oriented Programming (OOP) is a style of programming in which your code is broken up into units, known as</question>
<answer>objects</answer>
<answercorrect="yes">objects and classes</answer>
<answer>classes</answer>
<answer>objects or classes</answer>
</mchoice>
<mchoice>
<question>There are, however, some advanced features of C++, such as __ that are not supported in .NET, and are therefore, not available in managed code.</question>
<answer>Runtime classes</answer>
<answercorrect="yes">templates</answer>
<answer>base classes</answer>
<answer>multiple-document-interfaces</answer>
</mchoice>
<mchoice>
<question>Using C# you can, for example, write a ___ web page?</question>
<answer>dynamic</answer>
<answercorrect="yes">static</answer>
<answer>standard</answer>
<answer>classical</answer>
</mchoice>
<mchoice>
<question>Using C# you can, for example, write ___ of a distributed application?</question>
<answer>a core</answer>
<answer>an array</answer>
<answer>a structure</answer>
<answercorrect="yes">a component</answer>
</mchoice>
<mchoice>
<question> What actually manages your code ?</question>
<answer>JIT</answer>
<answercorrect="yes">CTS</answer>
<answer >CLR</answer>
<answer>CLS</answer>
</mchoice>
<mchoice>
<question>When the . NET runtime loads and runs code, this is the language that it expects to find the code in.</question>
<question> When you compile managed code, the ____ actually emits IL.</question>
<answer>debugger</answer>
<answer>collector</answer>
<answercorrect="yes">translator</answer>
<answer>compiler</answer>
</mchoice>
<mchoice>
<question> ____ this is a minimum set of standards that guarantees that code can be accessed from any language.</question>
<answercorrect="yes">Common Type System</answer>
<answer>Common Language Specification</answer>
<answer >Just-in-Time Compilation</answer>
<answer>The .NET Runtime</answer>
</mchoice>
<mchoice>
<question>Common Language Specification: This is a minimum set of standards that guarantees that code can be accessed from any</question>
<answer >compiler</answer>
<answer>stage</answer>
<answercorrect="yes">application</answer>
<answer>language</answer>
</mchoice>
<mchoice>
<question>Just-in-Time (JIT) Compilation: This is the term for the process of performing the ___ stage of compilation from IL into native machine code.</question>
<answercorrect="yes">intermediate</answer>
<answer>beginning</answer>
<answer >final</answer>
<answer>whole</answer>
</mchoice>
<mchoice>
<question>The function call generated by a non-OOP compiler causes what is called ___ binding</question>
<answer>weak</answer>
<answercorrect="yes">strong</answer>
<answer>early</answer>
<answer>late</answer>
</mchoice>
<mchoice>
<question>You create a string reference:</question>
<answer>string {s};</answer>
<answer>string [s];</answer>
<answer>string (s);</answer>
<answercorrect="yes">string s;</answer>
</mchoice>
<mchoice>
<question> When you create a reference, you want to connect it with a new object. You do so, in general, with the</question>
<answer>single quote</answer>
<answer>double quote</answer>
<answercorrect="yes">round brackets</answer>
<answer >new keyword</answer>
</mchoice>
<mchoice>
<question>There are six different places to store data. This is a general-purpose pool of memory (also in the RAM area) where all C# objects live.</question>
<answer>Registers</answer>
<answer>The heap</answer>
<answer >Static storage</answer>
<answercorrect="yes">The stack</answer>
</mchoice>
</quiz>
The user interface will allow application users to click on the "Start Quiz" button to start the quiz. The quiz will have 20 questions and each question may have more or one answers. The question with single answer should have radio boxes to select an answer and the questions with multiple choices should have check boxes to select the answers. In the end of the quiz, the results will be displayed to the user by percentage of correct answers and total questions. For example, if a user answers 16 questions correctly, he should see his result as 80%.
Then take one aspx page named as Default.aspx: Write the following code in aspx page.
In code behind Default.aspx.cs: This is coding part. Here I used view state for maintaining the state of all correct answers of questions and total answers selected by users etc...,please add System.Xml name space to your code behind page.