- Back to Home »
- SLIIT , WPF »
- How to Export an xml file from WPF application
Posted by : ජේ
Thursday, September 26, 2013
In my 3rd year project I had to export an xml file which contains data about questions for moodle. To do this task I use xml writer. So I am going to discuss how to read data from a DataGrid and export data as an xml file.
First add this headers
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Data;
System.Windows.Forms.SaveFileDialog sf = new System.Windows.Forms.SaveFileDialog();
sf.AddExtension = true;
sf.DefaultExt = "xml";
sf.ShowDialog();
if (sf.FileName != "")
{
using (XmlWriter writer = XmlWriter.Create(@sf.FileName))
{
writer.WriteStartDocument();
writer.WriteStartElement("quiz");
//add all true false questions
for (int r = 0; r < gridTrFaQues.Items.Count - 1; r++)
{
DataRowView row = (DataRowView)gridTrFaQues.Items[r];
writer.WriteStartElement("question");
writer.WriteAttributeString("type", "multichoice");
writer.WriteStartElement("name");
writer.WriteElementString("text", "True False Questions " + (i));
writer.WriteEndElement();
writer.WriteStartElement("questiontext");
writer.WriteAttributeString("format", "html");
writer.WriteElementString("text", row.Row.ItemArray[0].ToString());
writer.WriteEndElement();
writer.WriteStartElement("generalfeedback");
writer.WriteElementString("text", "general feedback");
writer.WriteEndElement();
writer.WriteElementString("defaultgrade", "1");
writer.WriteElementString("penalty", "0");
writer.WriteElementString("hidden", "0");
writer.WriteElementString("single", "true");
writer.WriteElementString("shuffleanswers", "true");
writer.WriteStartElement("correctfeedback");
writer.WriteElementString("text", "correct feedback");
writer.WriteEndElement();
writer.WriteStartElement("partiallycorrectfeedback");
writer.WriteElementString("text", "partiallycorrect feedback");
writer.WriteEndElement();
writer.WriteStartElement("incorrectfeedback");
writer.WriteElementString("text", "incorrect feedback");
writer.WriteEndElement();
writer.WriteStartElement("answer");
writer.WriteAttributeString("fraction", "100");
writer.WriteElementString("text", row.Row.ItemArray[1].ToString());
writer.WriteStartElement("feedback");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("answer");
writer.WriteAttributeString("fraction", "0");
writer.WriteElementString("text", row.Row.ItemArray[2].ToString());
writer.WriteStartElement("feedback");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("answer");
writer.WriteAttributeString("fraction", "0");
writer.WriteElementString("text", row.Row.ItemArray[3].ToString());
writer.WriteStartElement("feedback");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("answer");
writer.WriteAttributeString("fraction", "0");
writer.WriteElementString("text", row.Row.ItemArray[4].ToString());
writer.WriteStartElement("feedback");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
In this code I use a Save File Dialog to get the save point of the xml file. gridTrFaQues is the DataGrid name that I use to get data.
sample xml file for above code
<quiz>
<question type="multichoice">
<name>
<text>Common Questions 1</text>
</name>
<questiontext format="html">
<text>test ques</text>
</questiontext>
<generalfeedback>
<text>general feedback</text>
</generalfeedback>
<defaultgrade>1</defaultgrade>
<penalty>0</penalty>
<hidden>0</hidden>
<single>true</single>
<shuffleanswers>true</shuffleanswers>
<correctfeedback>
<text>correct feedback</text>
</correctfeedback>
<partiallycorrectfeedback>
<text>partiallycorrect feedback</text>
</partiallycorrectfeedback>
<incorrectfeedback>
<text>incorrect feedback</text>
</incorrectfeedback>
<answer fraction="100">
<text>1 answer</text>
<feedback/>
</answer>
<answer fraction="0">
<text>2 answer</text>
<feedback/>
</answer>
<answer fraction="0">
<text>3 answer</text>
<feedback/>
</answer>
<answer fraction="0">
<text>4 answer</text>
<feedback/>
</answer>
</question>
</quiz>
good find
ReplyDelete