c# - X文档文件名

标签 c# linq linq-to-xml

有没有办法从 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/

相关文章:

c# - 从日期列中获取按月过滤的行

c# - 如何从 IGrouping 中获取值

c#-3.0 - Linq to XML 文档遍历

linq-to-xml - linq 中的子对象

C# 泛型——为什么 lambda 有效,而简单的方法却无效?

c# - 连续的 XML 序列化原因 - EndRootElement 状态下的 token StartElement 将导致无效的 XML 文档

c# - 在没有数据重复的情况下转换/实例化/转换为子类/父类(super class)

c# - 有没有一种方法可以调整此功能使其不使用 LINQ? (将字节数组转换为字符串)

vb.net - 使用 LINQ 从 List(of ParentObject) 中查找重复的子项

c# - linq to xml 查询中返回的数据类型未知