Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Storing multiple items in XML (http://www.go4expert.com/forums/storing-multiple-items-xml-t4357/)

shah123 18May2007 20:48

Storing multiple items in XML
 
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());

                }


coderzone 21May2007 12:59

Re: Storing multiple items in XML
 
Have them in the items. Something like
Code:

<tasks>
  <task1>
  </task1>
  <task2>
  </task2>
</tasks>


shah123 21May2007 13:59

Re: Storing multiple items in XML
 
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>

shabbir 21May2007 14:46

Re: Storing multiple items in XML
 
You are already having multiple schedule

shah123 21May2007 14:52

Re: Storing multiple items in XML
 
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?

shabbir 21May2007 15:03

Re: Storing multiple items in XML
 
Loop through it.

shah123 21May2007 15:08

Re: Storing multiple items in XML
 
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();


shabbir 21May2007 15:16

Re: Storing multiple items in XML
 
Can you please tell the details of the error. You never mentioned the exception you are getting for the looping.

shah123 21May2007 15:31

Re: Storing multiple items in XML
 
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."}

shah123 21May2007 15:51

Re: Storing multiple items in XML
 
It worked. Thanks anyway just needed to put

sch.WriteStartDocument(); before for loop


All times are GMT +5.5. The time now is 01:11.