Storing multiple items in XML

shah123's Avatar
Contributor
Ok I got the XML File working but how i would store more then one schedule tasks in XML file as in above it can add 1 task at one time. What will be the looping condition?

Following is some of my code:
Code:
private void addBtn_Click(object sender, EventArgs e)
        {

            XmlTextWriter sch = null;
            sch = new XmlTextWriter(filename, null);
            sch.Formatting = Formatting.Indented;
            sch.Namespaces = false;



            if (subjTextbox.Text.Equals(""))
            {
                MessageBox.Show("Subject cannot be left Empty", "Subject");
            }
            else if (textFile.Text.Equals(""))
            {
                MessageBox.Show("File Name Needed", "File");
            }
            else
            {
                try
                {

                    date = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day, Convert.ToInt16(hr.Text), Convert.ToInt16(min.Text), 0);
                    if ((Convert.ToInt16(hr.Text))>=12)
                    {

                        lvt = new ListViewItem(new String[] { subjTextbox.Text, textFile.Text, date.ToString() +  "pm" });
                    }
                    else
                    {
                        lvt = new ListViewItem(new String[] { subjTextbox.Text, textFile.Text, date.ToString() +  "am" });
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Hour and Min fields are empty", ex.Message);

                    return;
                }
                if (IsInCollection(lvt))
                {
                    MessageBox.Show("Duplicate");
                }
                else
                {
                   
                    scheduleListView.Items.Add(lvt);
                   
                }
               
                try
                {
                   
                    sch.WriteStartDocument();

                    sch.WriteStartElement("Schedules");

                    sch.WriteStartElement("Scheduler");
                    sch.WriteString(subjTextbox.Text);
                    sch.WriteEndElement();

                    sch.WriteStartElement("FileName");
                    sch.WriteString(textFile.Text);
                    sch.WriteEndElement();

                    sch.WriteStartElement("DateTime");
                    sch.WriteString(date.ToString());
                    sch.WriteEndElement();

                   

                    sch.WriteEndElement();
                    sch.Flush();
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.ToString());

                }
0
coderzone's Avatar, Join Date: Jul 2004
Team Leader
Have them in the items. Something like
Code:
<tasks>
  <task1>
  </task1>
  <task2>
  </task2>
</tasks>
0
shah123's Avatar
Contributor
I didnt understood what you mean? But my basic query was "how would you insert multiple items from listview in XML. Items which will be selected and inserted in ListView should be able to store in XMl file"
e.g.
<schedule>

<schedule name>a</schedule name>
<file>c:\jjd.exe</file>
<date>23/05/2007 11:30pm</date>

<schedule name>b</schedule name>
<file>c:\jj.exe</file>
<date>23/05/2007 2:30pm</date>

</schedule>
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You are already having multiple schedule
0
shah123's Avatar
Contributor
No..By above i wanted to say e.g. You have ListItem and you are adding different tasks to it and those tasks should also be added in XML file as well so what can be done. As i found a way to add tasks from ListView but it adds only one time not like above. So my query is how to add multiple items to XML file to show same behaviuour as above?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Loop through it.
0
shah123's Avatar
Contributor
Thats what I am asking I looped but i am getting error on follwoing line. It says its being in use IO Exception.

XmlTextWriter sch = null;
sch = new XmlTextWriter(filename, null); // ERROR
sch.Formatting = Formatting.Indented;
sch.Namespaces = false;
sch = new XmlTextWriter(filename, null);

The way I looped is:
Code:
for (int i = 0; i < scheduleListView.Items.Count; i++)
                    {
                        sch.WriteStartDocument();

                        sch.WriteStartElement("Schedules");

                        sch.WriteStartElement("Scheduler");
                        sch.WriteString(subjTextbox.Text);
                        sch.WriteEndElement();

                        sch.WriteStartElement("FileName");
                        sch.WriteString(textFile.Text);
                        sch.WriteEndElement();

                        sch.WriteStartElement("DateTime");
                        sch.WriteString(date.ToString());
                        sch.WriteEndElement();

                        sch.WriteEndElement();
                    }
                        sch.Flush();
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Can you please tell the details of the error. You never mentioned the exception you are getting for the looping.
0
shah123's Avatar
Contributor
sch = new XmlTextWriter(filename, null);
IO exception was unhandled.
{"The process cannot access the file 'C:\\schedulers.xml' because it is being used by another process."}
0
shah123's Avatar
Contributor
It worked. Thanks anyway just needed to put

sch.WriteStartDocument(); before for loop