有没有办法从 XDocument 对象本身检索 XDocument 保存的路径(文件名)?
我的意思是在我已经保存 XDocument 对象之后获取保存的路径。像这样:
XDocument xDoc = new XDocument();
xDoc.Save(@"C:\Temp\MyXmlDoc.xml");
string str = xDoc.SavedPath() // <== something like this
最佳答案
如果加载 XDocument
从文件中,BaseUri
属性将包含文件名。如 MSDN 中所述:
Sometimes the XmlReader has the base URI, and sometimes it does not. For instance, when loading from a file, the XmlReader knows the base URI, but when reading from an XmlReader that was created because of calling the Parse method, there is no possibility of the XmlReader reporting a base URI; the XML was in a string.
但是,保存文档时不会设置,仅在加载操作期间设置。因此,如果您需要知道保存路径,则需要在保存时独立于 XDocument
实例进行存储。
关于c# - X文档文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5898583/