如何将XDocument的XML内容保存为.xml文件? [英] How can I save the XML contents of an XDocument as an .xml file?

查看:207
本文介绍了如何将XDocument的XML内容保存为.xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XDocument类,其中已经创建了XML内容。我基本上想要打开一个SaveFileDialog,让用户选择一个文件夹(不是文件),将内容保存为.xml文件。

I have an XDocument class with the XML contents already made. I basically want to open a SaveFileDialog, have the user choose a folder (not a file) in which to save the contents as an .xml file.

我有这样做有些困难:

a)如何使用SaveFileDialog提示用户选择文件夹?我只能用它来让用户选择一个文件。

a) How can I use the SaveFileDialog to prompt the user to select a folder? I've only been able to use it to get a user to select a file.

b)如何从SaveFileDialog中提取所选路径?

b) How do I extract the chosen path from SaveFileDialog?

c)一旦我有了路径,我该如何保存XDocument的内容?有一个名为Save的方法需要一个Stream - 如何使用路径构建流? (这可能是一个基本问题,我几乎没有IO经验)

c) Once I have the path, how can I save the contents of the XDocument? There's a method called Save that requires a Stream - how do I build the stream using the path? (This might be a basic question, I have almost no IO experience)

推荐答案

a)你不想选择一个文件夹,但文件名(保存* 文件 *对话框)

a) You don't want to select a Folder, but a file name (Save*File*Dialog)

b)SaveFileDialog.FileName

b) SaveFileDialog.FileName

c)查看不同的重载:你有XDocument.Save(string fileName)。不需要有一个流,你可以有一个fileName(哦,你在SaveFileDialog中得到它)

c) Look at different overloads : you have XDocument.Save(string fileName). No need to have a stream, you can have a fileName (oh, you got it in SaveFileDialog)

编辑:你的意思是用户可以' t更改文件的名称?
然后

EDIT : you mean user can't change the name of the file ? then

a)FolderBrowserDialog

a) FolderBrowserDialog

b)FolderBrowserDialog.SelectedPath

b) FolderBrowserDialog.SelectedPath

c)XDocument.Save(FolderBrowserDialog.SelectedPath +/+ THENAMEOFYOURFILETHATUSERCANTCHANGE)

c) XDocument.Save(FolderBrowserDialog.SelectedPath + "/" + THENAMEOFYOURFILETHATUSERCANTCHANGE)

编辑2 : Path.Combine在c))中更优雅。

(EDIT 2 : Path.Combine is more elegant in c) ).

这篇关于如何将XDocument的XML内容保存为.xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆