1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Storing multiple items in XML

Discussion in 'C#' started by shah123, May 18, 2007.

  1. shah123

    shah123 New Member

    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());
    
                    }
    
     
  2. coderzone

    coderzone Super Moderator

    Have them in the items. Something like
    Code:
    <tasks>
      <task1>
      </task1>
      <task2>
      </task2>
    </tasks>
     
  3. shah123

    shah123 New Member

    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>
     
  4. shabbir

    shabbir Administrator Staff Member

    You are already having multiple schedule
     
  5. shah123

    shah123 New Member

    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?
     
  6. shabbir

    shabbir Administrator Staff Member

    Loop through it.
     
  7. shah123

    shah123 New Member

    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();
    
     
  8. shabbir

    shabbir Administrator Staff Member

    Can you please tell the details of the error. You never mentioned the exception you are getting for the looping.
     
  9. shah123

    shah123 New Member

    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."}
     
  10. shah123

    shah123 New Member

    It worked. Thanks anyway just needed to put

    sch.WriteStartDocument(); before for loop
     
  11. shabbir

    shabbir Administrator Staff Member

    I would suggest you first investigate the issue as much as possible and try giving the details specific to the error.
     
  12. shah123

    shah123 New Member

    ok..no worries.
     

Share This Page