Share via


How to embed XML file in project resources?

Question

Sunday, May 16, 2010 7:05 PM | 1 vote

Hi

I want to embed XML file in the project resources so that I wont need to change the file path of the file every time and I move the project from one computer to another.

An Example:

 

Instead of writing this 

 XmlDocument data = new XmlDocument();
 data.Load("C:\\Users\\Tamer\\Documents\\Visual Studio 2008\\Projects\\TestDataRetrival\\TestDataRetrival\\Users.xml");

I want to write

 XmlDocument data = new XmlDocument();
      data.Load("Users.xml");

or any thing so that I wont write the full file path.

 

Thanks.

 

All replies (4)

Sunday, May 16, 2010 9:12 PM ✅Answered | 1 vote

If you want the xml file to be part of your dll as a resource, then you simply set the "Build Action" property of the xml file to "Embedded Resource".  To access it in code, then you use:

 

            System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
            XmlDocument doc = new XmlDocument();
            doc.Load(a.GetManifestResourceStream("WindowsFormsApplication8.Users.xml"));

 

This of course, assumes your project name is WindowsFormsApplication8.  The default namespace proceeds the filename in the resources.

--
Mike


Tuesday, May 18, 2010 8:24 AM ✅Answered

Hello tamerkhalil,
Thanks for your post. You could also try this:
1.Make sure the XML file is part of your .cs project.
2.Set the "Build Action" property for the XML file to "Embedded Resource".
3.Use the following code to retrieve the file contents at runtime:

public string GetResourceTextFile(string filename) {
 string result = string.Empty; 
 using (Stream stream = this.GetType().Assembly.GetManifestResourceStream("assembly.folder."+filename))
{
    using (StreamReader sr = new StreamReader(stream))
    {
        result = sr.ReadToEnd();
    }
}
return result;
Whenever you want to read the file contents, just use
string fileContents = GetResourceTextFile("Users.xml");

If you have any problems, please feel free to follow up.
Best regards,
Liliane

Please mark the replies as answers if they help and unmark them if they provide no help. Thanks


Sunday, May 16, 2010 7:58 PM

Use    

 XmlDocument data = new
 XmlDocument();

 data.Load(Environment.CurrentDirectory + "\\Users.xml"
);

And wherever the program runs from, it will be set.
My .NET Blog: http://michaelcrump.net


Saturday, July 19, 2014 11:01 PM

If you want the xml file to be part of your dll as a resource, then you simply set the "Build Action" property of the xml file to "Embedded Resource".  To access it in code, then you use:

 

            System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
            XmlDocument doc = new XmlDocument();
            doc.Load(a.GetManifestResourceStream("WindowsFormsApplication8.Users.xml"));

 

This of course, assumes your project name is WindowsFormsApplication8.  The default namespace proceeds the filename in the resources.

--
Mike

The first and third statements can be combined, like so.

            XmlDocument xmlASCIITable = new XmlDocument ( );            xmlASCIITable.Load ( System.Reflection.Assembly.GetExecutingAssembly ( ).GetManifestResourceStream ( "WizardWrx.WizardWrx.SharedUtl4.ASCII_Character_Display_Table.xml" ) );

Now, all I need is to work out how to combine this with a class generated by xsd.

David A. Gray Irving, Texas, USA http://www.wizardwrx.com/