11

I'm trying to embed an XML file into a C# console application via Right clicking on file -> Build Action -> Embedded Resource.

How do I then access this embedded resource?

XDocument XMLDoc = XDocument.Load(???);

Edit: Hi all, despite all the bashing this question received, here's an update.

I managed to get it working by using

XDocument.Load(new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Namespace.FolderName.FileName.Extension")))

It didn't work previously because the folder name containing the resource file within the project was not included (none of the examples I found seemed to have that).

Thanks everyone who tried to help.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Pupper
  • 2,315
  • 2
  • 22
  • 29
  • 2
    Do you mean something like [this](http://support.microsoft.com/kb/319292) or [these possible duplicates](https://www.google.com/#sclient=psy-ab&hl=en&source=hp&q=accessing+embedded+resources+c%23+site:stackoverflow.com&pbx=1&oq=accessing+embedded+resources+c%23+site:stackoverflow.com&aq=f&aqi=&aql=&gs_sm=e&gs_upl=3304l10983l0l11177l29l26l2l0l0l0l384l5613l1.14.7.4l28l0&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=4fe41488848db57f&biw=1366&bih=677)? – M.Babcock Jan 20 '12 at 04:13
  • possible duplicate of [How can I discover the "path" of an embedded resource?](http://stackoverflow.com/questions/27757/how-can-i-discover-the-path-of-an-embedded-resource) – John Leidegren Jan 21 '12 at 06:44
  • 1
    Exactly what I was looking for. It's unfortunate that it was closed as too localized. – Chuck Conway Oct 26 '13 at 04:44
  • 5
    The SO mods ate too many bananas?! What's localized with this question? – Sascha Dec 03 '13 at 15:28

1 Answers1

14

Something along these lines

using System.IO;
using System.Reflection;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var stream =  Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApplication1.XMLFile1.xml");
            StreamReader reader = new StreamReader(stream);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reader.ReadToEnd());
        }
    }
}

Here is a link to the Microsoft doc that describes how to do it. http://support.microsoft.com/kb/319292

Kerry H
  • 661
  • 5
  • 7