1

I'm hoping someone can help me with this annoying little problem I'm having. I'm trying to write a file path to an XML settings file, but nothing happens. I don't get any error messages or Just in Time debugging windows, it just doesn't execute that code block.

The program feature is one where you setup user accounts, and select a file for each account. If I leave the file paths blank, the XML file is created with no problems. However, if there is even one path to be written the file never even gets created.

Here is a sample of my function:

private void SaveSettings()
    {
        XmlWriterSettings xml_settings = new XmlWriterSettings();
        xml_settings.Indent = true;
        xml_settings.IndentChars = ("    ");

        using (XmlWriter xml_settings_file = XmlWriter.Create("settings.xml", xml_settings))
        {
            xml_settings_file.WriteStartElement("Main_Node");
            xml_settings_file.WriteElementString("SettingA", Properties.Settings.Default.SettingA.ToString());
            xml_settings_file.WriteElementString("SettingB", Properties.Settings.Default.SettingB.ToString());
            xml_settings_file.WriteElementString("SettingC", Properties.Settings.Default.SettingC.ToString());

            for (int i = 1; i < Properties.Settings.Default.UserAccounts.Count; i++)
            {
                xml_settings_file.WriteElementString("Account", System.Security.SecurityElement.Escape(Properties.Settings.Default.UserAccounts[i].ToString()));
                xml_settings_file.WriteElementString("File", System.Security.SecurityElement.Escape(Properties.Settings.Default.FilePaths[i]));
            }

            xml_settings_file.WriteEndElement();
            xml_settings_file.Flush();
        }

    }

To make things more confusing, when I replace the FilePath variable with a simple "Test" string, it works fine and the file is created with no problems.

8
  • you need to encode xml entities, not escape Commented Jan 13, 2012 at 13:50
  • What happens when you step through the code? Does it call your SaveSettings method? Does it get to the loop and then mysteriously quit? Does it run any code that might run after this point or does it terminate all processing there? Commented Jan 13, 2012 at 13:51
  • I don't see anything terribly wrong beside your for starting with 1 and double escaping of File and Account values. Are you sure SaveSettings is being ran at all? Can you walk through in the debugger and see where it stops? Commented Jan 13, 2012 at 13:53
  • I'm starting the loop at 1 because there is a dummy node at position 0 that I don't want to be written to the file. Commented Jan 13, 2012 at 14:09
  • Chris: I'm not 100% sure how to "step" through the code, but I placed a messagebox after the using statement and the message still pops up, but no file gets created. It doesn't even make the file with the first few settings. Commented Jan 13, 2012 at 14:14

2 Answers 2

1

Please try using CDATA instead of escaping path characters.

You can replace:

xml_settings_file.WriteElementString("File", System.Security.SecurityElement.Escape(Properties.Settings.Default.FilePaths[i]));

with following lines:

xml_settings_file.WriteStartElement("File");
xml_settings_file.WriteCData(Properties.Settings.Default.FilePaths[i]);
xml_settings_file.WriteEndElement();
Sign up to request clarification or add additional context in comments.

1 Comment

This didn't do anything. Still getting the same result.
0

This has yet to be solved. I ended up just using the built-in application settings instead.

1 Comment

Not really an answer... I'm having the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.