XDocument

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

Is it possible to get the path of a XDocument?

I am passing a XDocument to a method.
On that method I have some Linq queries applied to the XDocument and
then I need to save it:
MyDocument.Save(// ???)

I am trying to save it without passing the Path to the method. Is this
possible?

Thanks,
Miguel
 
Hello,

Is it possible to get the path of a XDocument?

I am passing a XDocument to a method.
On that method I have some Linq queries applied to the XDocument and
then I need to save it:
MyDocument.Save(// ???)

I am trying to save it without passing the Path to the method. Is this
possible?

Depends on exactly how the XDocument was created. But the BaseUri
property will contain the filename for the original document when possible.
 
Peter said:
Depends on exactly how the XDocument was created. But the BaseUri
property will contain the filename for the original document when possible.

Only when explicitly loading with
XDocument.Load("foo.xml", LoadOptions.SetBaseUri)
I think. Doing
XDocument.Load("foo.xml")
does not set the BaseUri.
 
[...]
Depends on exactly how the XDocument was created. But the BaseUri
property will contain the filename for the original document when
possible.

Only when explicitly loading with
XDocument.Load("foo.xml", LoadOptions.SetBaseUri)
I think. Doing
XDocument.Load("foo.xml")
does not set the BaseUri.

Yes and no. What you're writing is true for each individual object within
the document tree. But if you have the XDocument, you don't need BaseUri
to be set on each individual node; you just need the value from XDocument,
which is set if it can be.

Pete
 
Depends on exactly how the XDocument was created.  But the BaseUri  
property will contain the filename for the original document when possible.

Yes, I tried myDocument.Save(Assets.BaseUri); before but it didn't
work.

Now I followed Martin tip and opened as follows:

public class AssetRepository : IAssetRepository {
private XDocument Assets;

public String Path {
get { return _Path; }
set {
_Path = value;
Assets = XDocument.Load(String.Concat(Path, "Assets.xml"),
LoadOptions.SetBaseUri);
// ... Some other loading
}
} private string _Path;

public AssetRepository(String path) {
Path = path;
}

Then on a method I have:

String test =Assets.BaseUri;
// Some linq code ...
Assets.Save(Assets.BaseUri);

I get the following error on saving: URI formats are not supported.

I debugged and test is:
"file:///C:/Users/Miguel/Projects/Sti/App_Data/Assets.xml"

The path is correct.

Why this is not working?
Am I missing something?
 
shapper said:
Then on a method I have:

String test =Assets.BaseUri;
// Some linq code ...
Assets.Save(Assets.BaseUri);

I get the following error on saving: URI formats are not supported.

I debugged and test is:
"file:///C:/Users/Miguel/Projects/Sti/App_Data/Assets.xml"

The path is correct.

Why this is not working?

I guess the Save method wants a Windows file path, not a file: URI. So
you probably need
Uri fileUri = new Uri(Assets.BaseUri);
Assets.Save(fileUri.LocalPath);
 
Back
Top