Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XDocument File Name

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
like image 581
Roi Shabtai Avatar asked Dec 28 '25 16:12

Roi Shabtai


1 Answers

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.

like image 123
Jeff Yates Avatar answered Dec 30 '25 06:12

Jeff Yates



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!