Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a file that is in a the mapped folder "Layouts" in my Visual Studio project. I would like to read the contents of this file but I have been having some trouble accessing it. What I have tried:

        SPWeb web = SPContext.Current.Web;
        XmlTextReader reader = new XmlTextReader(web.Url + "/_layouts/MyPoject/data.xml");

But when i get to the reader.Read(); link it throws a 404 error. Is there a different way that I should be accessing this file?

share|improve this question
basic question: are you sure your url is correct ? (MyPoject instead of MyProject) –  Ziad WAKIM Nov 22 '11 at 8:23
try to get the file with the url in browser and see if path is correct –  Amit Kumawat Nov 22 '11 at 10:23
Check this as well : social.technet.microsoft.com/Forums/en-US/sharepointadmin/… –  Amit Kumawat Nov 22 '11 at 10:35
@ZiadWAKIM good catch. Unfortunately the project name is right. I changed it before I posted it here and must have mistyped. –  Abe Miessler Nov 22 '11 at 16:08
add comment

4 Answers

Since your file in _layouts directory, why not to use GetGenericSetupPath method? Something like this:

var path = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\MyPoject\data.xml");


This method is obsolete in SharePoint 2013, you should use GetCurrentGenericSetupPath or GetVersionedGenericSetupPath

Thanks for noticing that fact in comments.

share|improve this answer
Doesn't that just get me a path? I need to actually read the file. –  Abe Miessler Nov 23 '11 at 17:08
try this code: var path = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\MyPoject\data.xml"); XmlTextReader reader = new XmlTextReader(path);reader.Read(); using this method you open file by its physical path, not by uri. but it depends on your needs. –  Kai Nov 23 '11 at 19:48
I know this is old but people will find it on Google. This is now obsolete in SP2013. msdn.microsoft.com/en-us/library/… –  trgraglia Feb 20 at 8:43
Can confirm. Did find on Google. Update very useful. Thankyou x –  James Love Mar 28 at 11:10
add comment

Basically, you need to pass your default credentials for this to work.

Check this post where a text file is read from a layouts folder. I believe you are having similar issue.

share|improve this answer
add comment

Since you are putting the file in layouts, can I assume this is a file that going to remain static, and not be edited?

If that is the case you might like to try an alternative method, and that is to take advantage of the .NET resource framework.

Rather than put the xml file in the layouts folder, create a resources file in your VS project (eg Strings.resx)

Open the file and from the designer, select files from the top-left dropdown.

Drag and drop your xml file onto the designer. You now have the ability to access the content of the file as a string by simply using object.property form, for example

string xmlcontent = Strings.XmlFile1;

Once you have the xml in a string variable, you can do what you like with it eg load into and XmlDocument object.

Hope that makes sense.

share|improve this answer
add comment

Try getting it via webrequest:

WebRequest request = WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();

XmlTextReader = new XmlTextReader(response.GetResponseStream()))

Remember disposing ;)

share|improve this answer
Hi @pyranha, Its the same solution that is provided in the link above. –  Deepu Nair Nov 22 '11 at 9:22
oops didnt see that. copied it from one of my projects. if this didn't help check the url. probably use a absolute url for testing. –  pyranha Nov 22 '11 at 10:38
What would I dispose? –  Abe Miessler Nov 23 '11 at 17:09
Dispose the XmlTextReader Object and the WebResponse Object. –  pyranha Nov 24 '11 at 9:34
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.