Reputation: 3085
Is there a way to retrieve XDocument saved path (file name) from the XDocument object itself?
I mean to get the saved path after I already saved the XDocument object. Something like this:
XDocument xDoc = new XDocument();
xDoc.Save(@"C:\Temp\MyXmlDoc.xml");
string str = xDoc.SavedPath() // <== something like this
Upvotes: 3
Views: 4677
Reputation: 62387
If you load the XDocument
from a file, the BaseUri
property will contain the file name. As stated in 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.
However, this is not set when saving the document, only during load operations. Therefore, if you need to know the save path, you will need to store that independently of the XDocument
instance when saving.
Upvotes: 6
Reputation: 217351
No, an XDocument does not remember where it has been saved to.
You have to remember the path yourself, e.g.
XDocument xDoc = new XDocument();
string str = @"C:\Temp\MyXmlDoc.xml";
xDoc.Save(str);
Upvotes: 2