SPWeb.GetFileAsString(URL) causing the problem, and throws COMException, if an XML file size is more than 10 MB

PROBLEM : SPWeb.GetFileAsString(URL) method throws COMException, if we pass XML file and its size is more than 10 MB


Instead of using SPWeb.GetFileAsString(URL), use SPFile.OpenBinary() method.

try {
string str = Url.Text;
using (SPSite portalSite = new SPSite(str)){
SPWeb web = portalSite.OpenWeb();
SPFile file = web.GetFile(str);
byte[] content = file.OpenBinary();
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string str1 = enc.GetString(content);
//string str1 = web.GetFileAsString(str);
MessageBox.Show("File fetched successfully");
catch(Exception ex){

Comments (3)
  1. DkmS says:

    I use that code:

    public static string TextOfFile(SPFile file) {

    byte[] bf = file.OpenBinary();


    return Encoding.GetEncoding(file.CharSetName).GetString(file.OpenBinary());


    return Encoding.UTF8.GetString(file.OpenBinary());


  2. public static string GetString(SPFile file)


    using(var reader = new StreamReader(file.OpenBinaryStream()))


                               return reader.ReadToEnd();



Comments are closed.

Skip to main content