2

Please tell me why these 2 don't give me the same XmlDocument object.

  1. I can successfully load an external xml file using this code and successfully process the xml file:

    var xElem = XElement.Load("Products.xml");
    
  2. Now I embed the xml file as embedded resources in the assembly instead:

    var assembly = Assembly.GetExecutingAssembly();
    var stream = assembly.GetManifestResourceStream(this.GetType(), "AppNameSpace.Products.xml");
    var xElem= new XmlDocument();
    xElem.Load(stream);
    

Aren't the 2 xElem's supposed to be the same? Both XmlDocument containing the same info in Products.xml?

What did I do wrong? Thanks.

If I want to get the same xElem as in scenario 1, what must I do in scenario 2?

4
  • What is the difference between the two objects? They should load exactly the same structure, unless your files are different. Commented Dec 23, 2011 at 4:38
  • @TomislavMarkovski The files are exactly the same. That's why I don't know what's wrong. Commented Dec 23, 2011 at 6:47
  • Where in your project structure is that "Products.xml" and where is the code you showed? The GetExecutingAssembly gets the dll of the executing code - if the xml file is in a different assembly, it will not be found. Commented Dec 23, 2011 at 9:15
  • @HansKesting I believe I have only one assembly. I will check again. Thanks. Commented Dec 23, 2011 at 19:17

2 Answers 2

2

Build your XML file as a resource i.e. set "Build Action" to "Embeded Resource" in your Properties window when you add the file to the project.

See the following links

Check if your image exists in the resources with this

thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string [] resources = thisExe.GetManifestResourceNames();

Edit: Ok I just checked, you need to set the "Build Action" to "Embeded Resource". Setting it to "Resource" will not work. Do this, and check for the name of the resource with the above code. I'm sure it will work.

Sign up to request clarification or add additional context in comments.

3 Comments

This is what I did and the resource couldn't be found. Definitely my mistake but I haven't figured out where.
Check if you have some other namespace set in your project properties. Something that is different to what you are accessing.
Or check the names for all resources in your project. See my answer edit for code and links.
2

"Products.xml" needs the applications namespace before it. Have you checked if the stream or the XML Data is null?

2 Comments

This is one of my errors. I am not sure it's the only one error in my code though. I am working on fixing this. Thanks.
I still have the problem after adding in the app namespace before the file name. stream is still null after GetManifestResourceStream().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.