Storing multiple items in XML

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

  1. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    Have them in the items. Something like
    Code:
    <tasks>
      <task1>
      </task1>
      <task2>
      </task2>
    </tasks>
     
  3. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You are already having multiple schedule
     
  5. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Loop through it.
     
  7. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    It worked. Thanks anyway just needed to put

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

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    ok..no worries.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice