Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Embedded Resource, .xml Cannot read 2

Status
Not open for further replies.

dseaver

IS-IT--Management
Jul 13, 2006
467
I added two xml files to a resource file called Settings.xml, since I don't plan on changing the XML files and don't want to depend on the user having them. I used to access the file via XmlDocument.Load(). I can't find for the life of me how to be able to read these resources in XmlDocument. For examples, files are named FileA.xml and FileB.xml. I know that I can go to MyProject.Settings.FileA, but I can load them into the XmlDocument. Thanks in advance
 
Code:
        static string ExtractResource( string resourceName)
        {
            //look for the resource name
            foreach( string currentResource in System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceNames(*))
                if ( currentResource.LastIndexOf( resourceName) != -1 )
                {
                    string fqnTempFile = System.IO.Path.GetTempFileName();
                    string path = System.IO.Path.GetDirectoryName( fqnTempFile);
                    string rootName= System.IO.Path.GetFileNameWithoutExtension(fqnTempFile);
                    string destFile = path + @"\" + rootName + "." + System.IO.Path.GetExtension( currentResource);

                    System.IO.Stream fs =
                    System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceStream*(currentResource);


                    byte[] buff = new byte[ fs.Length ];
                    fs.Read( buff, 0, (int)fs.Length);
                    fs.Close();


                    System.IO.FileStream destStream = new System.IO.FileStream ( destFile,FileMode.Create);
                    destStream.Write( buff, 0, buff.Length);
                    destStream.Close();


                    return destFile;
                }


                throw new Exception("Resource not found : " + resourceName);
         }

Please note that this is not my code and I take no credit for it - original can be found here:


Hope this helps a little.
 
Apparently the code doesnt compile as it is, if you remove the stars it should compile.
 
Moses that's complicated.

if you use an XmlTextReader then you can load the xml from a stream. The easiest way to do that is like so:

1. Make sure that your xmlfile has the Compile property set to Embedded Resource.

2. Get the stream from the manifest and load it into the xmltextreader.

Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("solutionname.yourprojectname.anyfoldername.FileA.xml");

XmlTextReader reader = new XmlTextReader(s);

now you can work with the reader directly.

The string that goes inside the GetManifestResourceStream is essentially a path starting at your solution name but the / is replaced with .

Note that if your file is contained within any folder that starts with a number, then you must put a _ before the folder name.

MySolution
MyProject
32BitFiles
MyFile.xml

Your string would be MySolution.MyProject._32BitFiles.MyFile.xml

 
I still can't get it working, but I think it has to do with how I have it set up. The stream is always NULL

Tell me if this is right:
So I add a folder, XMLs, to the project, MyProject, solution is same name as project
I add existing item, FileA.xml
I go to properties, Build = Embedded Resource

then I use
Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.MyProject.xmls.FileA.xml");

XmlTextReader reader = new XmlTextReader(s);

XmlDocument fileaXml = new XmlDocument();
filea.load(s);
 
Nevermind, I found it, using a combination of the solutions. JurkMonkey's solution is the one I am using, but to get the resource name correct I used djfrear's suggestion's
Code:
foreach( string currentResource in System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceNames(*))
to get the correct resource name. Thank you both!!
 
A shortcut would be to compile your assembly with your embedded resource, then use Reflector or IlDasm to look at it -- the resource name can then easily be seen.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
link for resourcer: http://www.aisto.com/roeder/dotnet/

download then extract to X:/Documents and Settings/<user area>/SendTo dir. You can then navigate to your projects dir then right-click > Send To > Resourcer to look up your projects resources.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top