LINQ to XML on assembly embedded XML files

LINQ to XML rocks. I use it pretty much day in day out to read mock up data from XML files for prototyping.
Today I needed a way to work with an XML file that was embedded in an assembly as an Embedded Resource.

Assembly embedded XML File

Following are the contents of the file:

 
  1. <Users>  
  2.   <User ID="1" Name="Jim"/>  
  3.   <User ID="1" Name="Jon"/>  
  4.   <User ID="1" Name="jack"/>  
  5.   <User ID="1" Name="Matt"/>    
  6. </Users>  

LINQ to XML makes working with this very easy.

 
  1. public static XDocument GetDocument(string docName)  
  2.         {  
  3.             using (Stream resourceStream = Assembly.GetExecutingAssembly()  
  4.                                                    .GetManifestResourceStream(docName))  
  5.             {  
  6.                 using (StreamReader resourceStreamReader = new StreamReader(resourceStream))  
  7.                 {  
  8.                     String xml = resourceStreamReader.ReadToEnd();  
  9.                     XDocument doc = XDocument.Parse(xml);  
  10.                     return doc;  
  11.                 }  
  12.             }  
  13.         }  

We get a handle to the underlying XML file stream using GetManifestResourceStream and pass it to a StreamReader.
We read the contents of the entire XML file by calling ReadToEnd(). At this point we have the entire XML file loaded into a string.
We simply parse the string to create a XDocument and from here we are in familiar territory of working with LINQ to XML just as we know and love.
The using statements ensure that the underlying streams are disposed off gracefully.

 
  1. public static void Main(string[] args)  
  2.         {  
  3.             XDocument doc = GetDocument("CSharp.Users.xml");  
  4.             var users =  doc.Element("Users").Elements("User").Select  
  5.                  (x => new  
  6.                      {  
  7.                          ID = (string)x.Attribute("ID"),  
  8.                          Name = (string)x.Attribute("Name")  
  9.                      }  
  10.                  );  
  11.   
  12.             foreach (var user in users)  
  13.             {  
  14.                 Console.WriteLine("ID:{0} Name:{1}",user.ID,user.Name);  
  15.             }  
  16.         }  

One thing to be aware of is that the name of the XML file needs to be prefixed with the name of the assembly.
(CSharp in this case). So the fully qualified XML file name is "CSharp.Users.xml".

The output shows that we were able to read and parse the file successfully.
Output
That's all there is to it.Happy LINQing...

kick it on DotNetKicks.com

Print | posted @ Tuesday, March 31, 2009 9:57 PM

Comments on this entry:

Gravatar # re: LINQ to XML on assembly embedded XML files
by rajesh at 9/1/2009 5:53 AM

you rock! nice post...was waiting for this code
Gravatar # re: LINQ to XML on assembly embedded XML files
by Bruno at 5/24/2010 9:40 AM

Nice! Thanks for sharing.
Post A Comment
Title:
Name:
Email:
Comment:
Verification: