Hi All,

I am trying to create an XML file that saves email addresses and files. This would then be read by another application and will send the emails and attach the files to the email.

I am using the OpenFileDialog to get the file path and name. But when I try to save the XML file after creating it it does not save. There are no errors being produced it just does not save the file. If I type the exact same file path in manually then the file ssaves with no problems.

string[] reportFileNameArray;
            oFDAddReport.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
            oFDAddReport.Filter = "Word Document|*.doc|Adobe|*.pdf|Text File|.txt|Rich Text File|*.rtf|All Files|*.*";
            oFDAddReport.DefaultExt = "Word Document|*.doc";

            if (oFDAddReport.ShowDialog() != DialogResult.Cancel)
            {
                try
                {
                    int indexItem;
                    indexItem = cboTitle.SelectedIndex;
                    mailGroup = (MailGroup)listMailGroups[indexItem];
                    reportFileNameArray = oFDAddReport.FileNames;
                    foreach (string reportFileName in reportFileNameArray)
                    {
                        mailGroup.Reports.Add(reportFileName);
                        lstReports.Items.Add(reportFileName);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

The mailGroup.Report is an arrayList of Strings. When I step through the saving function the string is in the mailGroup object and appears correct, with the appropiate escape charaters for the "\"'s in the path.


The code used for saving is below.

XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;            
                settings.ConformanceLevel = ConformanceLevel.Auto;
                settings.IndentChars = "\t";
                settings.OmitXmlDeclaration = false;
                settings.Encoding = System.Text.Encoding.UTF8;
                using (XmlWriter xmlWriter = XmlWriter.Create(dataFileRelative, settings))
                {
                    xmlWriter.WriteStartDocument();
                    xmlWriter.WriteStartElement("mail_group");
                    for (int i = 0; i < listEmailGroups.Count; i++)
                    {
                        MailGroup mailGroup = new MailGroup();
                        mailGroup = (MailGroup)listEmailGroups[i];
                        xmlWriter.WriteStartElement("group");
                        xmlWriter.WriteElementString("group_name", mailGroup.GroupName);
                        foreach(String recipient in mailGroup.Recipients)
                        {
                            xmlWriter.WriteElementString("recipients", recipient);
                        }
                        foreach (String report in mailGroup.Reports)
                        {
                            xmlWriter.WriteElementString("reports", report);
                        }
                        xmlWriter.WriteEndElement();
                    }
                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndDocument();
                }

I have added the xmlWrited.Close(); to the end of the save code but it still has the same issues. When I type in the file path (using a text box and another method) the string is identical but in this case the xml file now saves.

Edited 6 Years Ago by mikemoser: n/a

This article has been dead for over six months. Start a new discussion instead.