I cannot find any documentation from microsoft regarding what encoding is used to read the file content when using the SPWeb.GetFileAsString(url). Does anyone know?

  • What kind of file you are trying to return? Is it a XML file? – Varun Verma Jul 3 '14 at 6:24
  • I am just trying to understand what type of encoding is used as it is reading a file and returns a string, but without specifying the encoding. Could be an XML, XAML, ASPX(only content in it not a rendered page) in arbitrary encoding. – Robban1980 Jul 3 '14 at 6:37
  • see my answer below – Varun Verma Jul 3 '14 at 6:37

This method is used to get XML, XSL files.

You need to use XmlTextReader in order to get the contents from a file.

One example is listed below:

XmlTextReader reader = new XmlTextReader(new StringReader(web.GetFileAsString("URLHere")));

When you are dynamically adding some WebPart this method helps to associate XML, XSL to that WebPart.

This function returns a .Net string so probably it is UTF-16(Default to .Net) encoded. :)

| improve this answer | |
  • Not really, but it opened up new way of seeing how it should be used. It does still not answer what encoding is used to read the string that it returns from the file. How does the GetFileAsString return the correct formatting if it doesn't know if the content is UTF-8, Shift_JIS or ASCII encoding? In your example the XMLTextReader can be configured to use specific encodings however if the string is already messed up up by the GetFileAsString that would not make any difference. – Robban1980 Jul 3 '14 at 6:57
  • Updated my answer. This function returns .Net string. and .Net string is UTF16 encoded :) – Varun Verma Jul 3 '14 at 7:09

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.