locked
Saving data on listview to xml RRS feed

  • Question

  • I have an application that loads xml file into a listview at load.

    And I made a button to load an excel file using oledb and put that data into listview at runtime.

    Now, I am trying to save the data on listview back to the xml file (after some change).

    But strangely, xmlwriter saves the data only when the original xml is loaded in the beginning.

    I am using the following code to loop through my data and save in my xml file.

                    XmlWriterSettings settings = new XmlWriterSettings() { Indent = true, IndentChars = "  " };
    
                    using (XmlWriter writer = XmlWriter.Create(Directory.GetCurrentDirectory() + @"\PEL2.xml", settings))
    
                    {                    
    
                        writer.WriteStartDocument();
    
                        writer.WriteStartElement("PEL");
    
                        foreach (ListViewItem one in pelListView.Items)
    
                        {
    
                            writer.WriteStartElement("Employee");
    
                            writer.WriteElementString("CID", one.SubItems[0].Text);
    
                            writer.WriteElementString("DID", one.SubItems[1].Text);
    
                            writer.WriteElementString("EID", one.SubItems[2].Text);
    
                            writer.WriteEndElement();
    
                        }
    
                        writer.WriteEndElement();
    
                        MessageBox.Show("done");
    
                    }
    
    

    At load, the program reads PEL.xml and adds data to listview.

    I set it to PEL2 to see if it is saving correctly and it does if no changes are made to my listview.

    And yet when I go check the application path, the file is simply not there. I put this in try-catch block and caught no errors.

    Any ideas why?
    Monday, July 27, 2009 3:29 PM

Answers

  • Do you have permission to wrote to that directory?

    writer.Flush();
    write.Close();

    You can try these before you fall out of the using blocks to flush the buffer.
    • Marked as answer by Sungmo Yeon Tuesday, July 28, 2009 1:59 PM
    Monday, July 27, 2009 4:31 PM
  • Check the file name you're writing to. The current directory is not necessarily the directory where your program sits. You can get that using Application.StartupPath.


    Mark the best replies as answers!
    • Marked as answer by Sungmo Yeon Tuesday, July 28, 2009 1:59 PM
    Tuesday, July 28, 2009 7:12 AM

All replies

  • Do you have permission to wrote to that directory?

    writer.Flush();
    write.Close();

    You can try these before you fall out of the using blocks to flush the buffer.
    • Marked as answer by Sungmo Yeon Tuesday, July 28, 2009 1:59 PM
    Monday, July 27, 2009 4:31 PM
  • writer.Close();
    This should work as suggested by Michael Tx...


    Or try using 'XmlDocument' class to create a XmlFile...

    Regards,
    IamHuM
    Tuesday, July 28, 2009 5:56 AM
  • Check the file name you're writing to. The current directory is not necessarily the directory where your program sits. You can get that using Application.StartupPath.


    Mark the best replies as answers!
    • Marked as answer by Sungmo Yeon Tuesday, July 28, 2009 1:59 PM
    Tuesday, July 28, 2009 7:12 AM
  • Yes. How careless I had been.

    The files were saving on my Desktop, which I had way too many files to notice that they were being saved there.

    But I just removed that feature and moved on.

    Thank you Daniel.
    Tuesday, July 28, 2009 2:02 PM