Geeks With Blogs
Under The Influence(of code) Abhijeet Patel's blog

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
Posted on Tuesday, March 31, 2009 9:57 PM C# , LINQ | Back to top


Comments on this post: LINQ to XML on assembly embedded XML files

# re: LINQ to XML on assembly embedded XML files
Requesting Gravatar...
you rock! nice post...was waiting for this code
Left by rajesh on Sep 01, 2009 5:53 AM

# re: LINQ to XML on assembly embedded XML files
Requesting Gravatar...
Nice! Thanks for sharing.
Left by Bruno on May 24, 2010 9:40 AM

Your comment:
 (will show your gravatar)


Copyright © Abhijeet Patel | Powered by: GeeksWithBlogs.net | Join free