0

I'm trying to read xml files from assembly, this is my code:

private Assembly testAssembly;
    private void button1_Click(object sender, EventArgs e)
    {
        string testfile = "stack.xml";
        String testfileName = string.Format("{0}.{1}", testAssembly.GetName().Name, testfile);

        using (Stream stream = testAssembly.GetManifestResourceStream(testfileName))
        {
            using (StreamReader sr = new StreamReader(stream))
            {
                var value = sr.ReadToEnd();
                Console.WriteLine("The read file is {0}", value);
            }
        }
    }

I've got a problem on this line:

String testfileName = string.Format("{0}.{1}", testAssembly.GetName().Name, 
                                     testfile); 

Null ref exception unhandled, it says null. Any help??? enter image description here

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
linguini
  • 1,939
  • 5
  • 49
  • 79

2 Answers2

2

Looks like marksAssembly is null

TesterJeff
  • 1,626
  • 2
  • 16
  • 20
  • @linguini - Make sure its not `null` – Security Hound Oct 17 '12 at 14:49
  • @Ramhound: How can i do this?? – linguini Oct 17 '12 at 14:52
  • when the exception is throw, put the mouse over `testAssembly` it shoudl show you a little tooltip in which you can read if `testAssebmly` is `null` or not – Gianni B. Oct 17 '12 at 14:56
  • Yes, it says testAssembly is null. What should i do exactly?? – linguini Oct 17 '12 at 14:59
  • @Quick Joe Smith: What should i assign, the filename or something?? – linguini Oct 17 '12 at 15:00
  • You have to load the assembly! Check one of this [methods](http://msdn.microsoft.com/en-us/library/system.reflection.assembly.load.aspx) – Gianni B. Oct 17 '12 at 15:01
  • @ NaNNy: Could u please tell me what should i load and where?? – linguini Oct 17 '12 at 15:05
  • If the assembly that you want to load is in the reference of the project, you just need this in the click handler: `testAssembly = Assembly.Load("nameOfAssemblyWithoutTheExtension");`. If is not in the reference you can specify the path with `Assembly.LoadFile("PathOfYourAssembly")`. You can found an example [here](http://www.codeproject.com/Articles/34301/Assembly-LoadFile-versus-Assembly-LoadFrom-NET-obs) – Gianni B. Oct 17 '12 at 15:10
  • I got one project and I have loaded the xml file in the same project embeded it and there is no other assembly. – linguini Oct 17 '12 at 15:13
  • I really don't understand why you need to use `Assembly` when you have the xml loaded (and included) in your project... Check this two question: [one](http://stackoverflow.com/questions/6416564/how-to-read-a-text-file-in-projects-root-directory) and [two](http://stackoverflow.com/questions/8580526/get-file-path-for-file-included-in-project). – Gianni B. Oct 17 '12 at 15:19
1
string resName = "resources/HistoryConnections.xml";  

private static UnmanagedMemoryStream GetResourceStream(string resName)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var strResources = assembly.GetName().Name + ".g.resources";
            var rStream = assembly.GetManifestResourceStream(strResources);
            var resourceReader = new ResourceReader(rStream);
            var items = resourceReader.OfType<DictionaryEntry>();
            var stream = items.First(x => (x.Key as string) == resName.ToLower()).Value;
            return (UnmanagedMemoryStream)stream;
        }
Kampai
  • 22,848
  • 21
  • 95
  • 95