P
P
pimanov32021-12-25 07:35:54
WPF
pimanov3, 2021-12-25 07:35:54

How to specify the path to a file in WPF embedded resources?

I specify in XmlReader.Create the path to the xml file that I put in the resources.
61c69d95c30b6149079428.png
Tried some implementation methods, but ran into an error when running "URI prefix not recognized" or "Argument 1: Cannot convert from 'System.Uri' to 'string'".

XmlReader xml = XmlReader.Create(@"pack://application:,,,/Resources/data_on_calculation_types.xml");

XmlReader xml = XmlReader.Create(new Uri(@"pack://application:,,,/Resources/data_on_calculation_types.xml", UriKind.RelativeOrAbsolute));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-12-25
@pimanov3

Good afternoon.
In your example you used a non-WPF resource
, it can be accessed as a property (in my case a resource named Data_XMLFile is used):

string res=Properties.Resources.Data_XMLFile;
XmlReader xml2 = XmlReader.Create(new StringReader(res));

If you want to use a WPF resource then you need to select this xml file and set the build action to Resource
61c74b4dcdc71094191470.png
The resource can then be accessed like this:
Uri uri = new Uri("pack://application:,,,/Resources/Data_XMLFile.xml", UriKind.Absolute);
StreamResourceInfo info = Application.GetResourceStream(uri);            
XmlReader xml = XmlReader.Create(info.Stream);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question