使用 XmlWriter 将文件路径写入 XML 文件
我希望有人能帮助我解决我遇到的这个恼人的小问题。我试图将文件路径写入 XML 设置文件,但没有任何反应。我没有收到任何错误消息或即时调试窗口,它只是不执行该代码块。
该程序功能是您设置用户帐户并为每个帐户选择一个文件。如果我将文件路径留空,则创建 XML 文件不会出现任何问题。但是,如果只有一个要写入的路径,则该文件根本不会被创建。
这是我的函数的示例:
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();
}
}
为了让事情变得更加混乱,当我用简单的“Test”字符串替换 FilePath 变量时,它工作正常并且创建文件没有任何问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试使用 CDATA 而不是转义路径字符。
您可以将: 替换
为以下几行:
Please try using CDATA instead of escaping path characters.
You can replace:
with following lines:
这个问题还有待解决。我最终只是使用内置的应用程序设置。
This has yet to be solved. I ended up just using the built-in application settings instead.