c# - How to add a resource : xml file -


i'm using following xaml code add xml file resource :

<xmldataprovider x:key="mydata"  source="d:\myfile.xml" xpath="books/book" /> 

now use myfile.xml %appdata% folder, more portability. obviously, using source="%appdata%\myfile.xml" doesn't work, seems "%" char not accepted in xaml.

thus want add resource programmatically in main window code. i've tried :

public mainwindow() {     this.resources.add("mydata", "d:\\myfile.xml");     initializecomponent(); } 

but don't have xpath="" property available in resources.add method, hence application doesn't work correctly.

is there way set property ? or doing wrong ?

no google answer this.

thank much.

you can use snippet in codebehind:

public mainwindow() {     xmldataprovider provider = new xmldataprovider()     provider.source = new uri(environment.getfolderpath(environment.specialfolder.applicationdata) + "\\myfile.xml");     provider.xpath = "books/book";     this.resources.add("mydata", provider); } 

this correct code add resources since

 this.resources.add("mydata", "d:\\myfile.xml"); 

will add recources string key "mydata" , value "d:\myfile.xml"

or can still use xaml declaration , in constructor add:

(this.resources["mydata"] xmldataprovider).source = environment.getfolderpath(environment.specialfolder.applicationdata) + "\\myfile.xml"; (this.resources["mydata"] xmldataprovider).xpath = "books/book"; 

i suggest last solution.


Comments