3

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");
        reader.Read();

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?

4

5 Answers 5

12

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

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

UPD:

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

Thanks for noticing that fact in comments.

4
  • Doesn't that just get me a path? I need to actually read the file. Nov 23, 2011 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. Nov 23, 2011 at 19:48
  • 3
    I know this is old but people will find it on Google. This is now obsolete in SP2013. msdn.microsoft.com/en-us/library/… Feb 20, 2014 at 8:43
  • 1
    Can confirm. Did find on Google. Update very useful. Thankyou x
    – James Love
    Mar 28, 2014 at 11:10
1

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.

1

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.

0

Try getting it via webrequest:

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

XmlTextReader = new XmlTextReader(response.GetResponseStream()))
reader.Read();

Remember disposing ;)

4
  • Hi @pyranha, Its the same solution that is provided in the link above.
    – Deepu Nair
    Nov 22, 2011 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, 2011 at 10:38
  • What would I dispose? Nov 23, 2011 at 17:09
  • Dispose the XmlTextReader Object and the WebResponse Object.
    – pyranha
    Nov 24, 2011 at 9:34
0

This works for me:

string path = HttpContext.Current.Server.MapPath(xslFileUrl);           
return File.ReadAllText(path);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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