Reputation: 6979
I have a Silverlight application in which I am doing the following thing to load a XML file, and further to parse it.
I have set the Build Action to Embedded Resource, Copy to Output Directory.
The code that I am using is:
try
{
Xmlfile = XDocument.Load(Assembly.GetExecutingAssembly().GetManifestResourceStream("file.xml"));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Message that I receive as an exception:
Value cannot be null.
Parameter name: input
EDIT
I tried the following which works (in WPF), but gives problem in Silverlight:
Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + ".file.xml")
Error:
'System.Reflection.Assembly.GetName()' is inaccessible due to its protection level
Upvotes: 1
Views: 1020
Reputation: 578
I don't have an answer yet, but the prepend "solution" does not work for Silverlight 5 apps.
If you complie the proffered code withing a Silverlight 5.0 project you will get 'System.Reflection.Assembly.GetName()' is inaccessible due to its protection level at compile time.
For the record I get similar errors for:
System.Reflection.Assembly.GetName();
System.Reflection.Assembly.GetEntryAssembly()
<someAssembly>.GetReferencedAssemblies()
For the other GetName() overload you get the same protection error.
<someAssembly>.GetType(fullyQualifiedTypeName, false, true);
'System.Reflection.Assembly.GetType(string, bool, bool)' is inaccessible due to its protection level
I suspect the protection level error stems from the fact that Silverlight apps are only partially trusted code and Reflection by its nature is requires high trust. But, in the end I merely speculating about what is behind the Microsoft code wall.
Is there an article on MSDN or somewhere that describes which portions of C# reflection vis a vis Assemblies that is disabled/protect when incorporated into a Silverlight 5 web app?
Upvotes: 1
Reputation: 102783
You will need to prepend the name of your assembly and its subfolders, like this:
string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
XDocument.Load(assemblyName + ".subfolder.file.xml");
Sometimes it's not clear exactly what the subfolder(s) should be. If that's the case, just inspect the names directly using this:
string[] names = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
Upvotes: 1