C# Filestream不会写入包含OpenFileDialog项的xmldoc
我有一段代码,它采用OpenFileDialog获得的文件路径,并尝试将其保存到xml文件中。出于某种原因,如果其中一个节点包含此打开文件对话框中的字符串,则不会写入xml文档。不会抛出异常,应用程序也不会崩溃,只是不会写入文件 如果我使用字符串文字代替具有相同内容的m_strSoundFile,那么xml文档将被正确写入。所以它与“\”字符非法无关,这是我最初的想法。也许这与OpenFileDialog是Win32这一事实有关?任何帮助都将不胜感激 谢谢, 亚历克斯C# Filestream不会写入包含OpenFileDialog项的xmldoc,c#,.net,filestream,xmldocument,openfiledialog,C#,.net,Filestream,Xmldocument,Openfiledialog,我有一段代码,它采用OpenFileDialog获得的文件路径,并尝试将其保存到xml文件中。出于某种原因,如果其中一个节点包含此打开文件对话框中的字符串,则不会写入xml文档。不会抛出异常,应用程序也不会崩溃,只是不会写入文件 如果我使用字符串文字代替具有相同内容的m_strSoundFile,那么xml文档将被正确写入。所以它与“\”字符非法无关,这是我最初的想法。也许这与OpenFileDialog是Win32这一事实有关?任何帮助都将不胜感激 谢谢, 亚历克斯 // ///MainWin
//
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
字符串m_strSoundFile;
公共主窗口()
{
初始化组件();
}
私有无效btnChooseFile_单击(对象发送方,路由目标)
{
Microsoft.Win32.OpenFileDialog dlg=新的Microsoft.Win32.OpenFileDialog();
dlg.Filter=“Wav文件(*.Wav)|*.Wav”//按扩展名筛选文件
dlg.InitialDirectory=@“C:\windows\media”;
可为空的结果=真;
bool-pathExists=false;
做
{
结果=dlg.ShowDialog();
如果(结果==真)
{
pathExists=dlg.CheckPathExists;
如果(!pathExists)
MessageBox.Show(“路径不存在”);
其他的
m_strSoundFile=dlg.FileName;
}
}while(result==true&&!pathExists);
m_tbFilename.Text=m_strSoundFile;
}
私有无效窗口\u关闭(对象发送方,System.ComponentModel.CancelEventArgs e)
{
XmlDocument xmlDoc=新的XmlDocument();
XmlNode xmlRootNode=xmlDoc.CreateElement(“设置”);
XmlNode节点=xmlDoc.CreateElement(“文件”);
xmldattribute a=xmlDoc.CreateAttribute(“路径”);
a、 值=m_strSoundFile;
node.Attributes.Append(a);
AppendChild(节点);
AppendChild(xmlRootNode);
System.IO.FileStream fs;
尝试
{
fs=System.IO.File.Open(“configfile.xml”,System.IO.FileMode.Create,System.IO.FileAccess.Write);
Save(XmlWriter.Create(fs,newxmlwritersettings(){Indent=true,Encoding=Encoding.UTF8}));
fs.Close();
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
}
好的,我想出来了。在为文件流使用绝对路径之后,它工作了。当不使用绝对路径时,它会有条件地工作,这仍然很奇怪。当你说“它不会被写入”时,你的意思是代码会抛出错误吗?或者代码以静默方式失败?如果是错误,您能包含错误消息吗?@DanPuzey不会抛出异常,代码将完全执行,填充不会写入。谢谢@亚历克斯,代码对我有用。有例外吗?您是否传递了文件的正确路径。Open
方法?@Serge我正在运行确切的代码。它不会抛出任何异常,我在debug->exceptions中启用了CLR异常。您是否在visual studio 2010中像我一样在64位xp机器上运行它?我不知道这有什么关系,只是好奇而已
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string m_strSoundFile;
public MainWindow()
{
InitializeComponent();
}
private void btnChooseFile_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Wav files (*.wav)|*.wav"; // Filter files by extension
dlg.InitialDirectory = @"C:\windows\media";
Nullable<bool> result = true;
bool pathExists = false;
do
{
result = dlg.ShowDialog();
if (result == true)
{
pathExists = dlg.CheckPathExists;
if (!pathExists)
MessageBox.Show("Path does not exist");
else
m_strSoundFile = dlg.FileName;
}
} while (result == true && !pathExists);
m_tbFilename.Text = m_strSoundFile;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode xmlRootNode = xmlDoc.CreateElement("Settings");
XmlNode node = xmlDoc.CreateElement("File");
XmlAttribute a = xmlDoc.CreateAttribute("Path");
a.Value = m_strSoundFile;
node.Attributes.Append(a);
xmlRootNode.AppendChild(node);
xmlDoc.AppendChild(xmlRootNode);
System.IO.FileStream fs;
try
{
fs = System.IO.File.Open("configfile.xml", System.IO.FileMode.Create, System.IO.FileAccess.Write);
xmlDoc.Save(XmlWriter.Create(fs, new XmlWriterSettings() { Indent = true, Encoding = Encoding.UTF8 }));
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}