title: Unable to read from XML file in a SharePoint Timer Job
body: <p>I have a custom timer job which should read some data from an XML file and save it in a word file. I tested this code in a simple web part and it works perfectly fine. However it is not working with custom timer job. Here is the code - </p>
  
  <pre><code>public override void Execute(Guid targetInstanceId)
      {
          #if (DEBUG)
              System.Diagnostics.Trace.Assert(false);
          #endif
  
  
          // get a reference to the current site collection's content database
          SPWebApplication webApplication = this.Parent as SPWebApplication;
          SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
  
  
          //Define a string builder object
          StringBuilder stringBuilder = new System.Text.StringBuilder();
          //double revenue = 0;
          //creating object of configuration class
  
          //Reading page size from xml configuration file
          int intPageLimit = weeklyConfig.readPageSize();
          //Reading column name from xml configuration file
          List&lt;string&gt; strList = weeklyConfig.readColumnName();
          //Reading list name from xml configuration file
          string strListName = weeklyConfig.readListName();
          //Reading Library name from xml 
          string strLibraryName = weeklyConfig.readLibraryName();
          stringBuilder.Append(intPageLimit);
          foreach (string str in strList)
          {
              stringBuilder.Append(str);
          }
          stringBuilder.Append(strListName);
          stringBuilder.Append(strLibraryName);
          SPFolder customerDocLib = null;
          customerDocLib = contentDb.Sites[0].RootWeb.Folders["Reports"];
          byte[] byteArray = Encoding.ASCII.GetBytes(stringBuilder.ToString());
          MemoryStream stream = new MemoryStream(byteArray);
          string fileName = "TestReport_" + DateTime.Now.ToString("MMddyyyyhhmmss") + ".doc";//Creating .doc file
          customerDocLib.Files.Add(fileName, stream);//writing memory stream to doc file and saving it into SharePoint document library
      }
  </code></pre>
  
  <p>Here is the code which creates the XML file. I have added this code in an Event Receiver at feature level. I'm saving this XML file in inetpub's virtual directory where my site collection is deployed. Path - C:/inetpub/wwwroot/wss/VirtualDirectories/38118/Configuration/ConfigFile.xml </p>
  
  <pre><code>public static void CreateConfigFile(String path)
      {
          //Use an xml Writer
          using (XmlWriter writer = XmlWriter.Create(path + "/ConfigFile.xml"))
          {
              //Start the Xml Document
              writer.WriteStartDocument();
              //Create Root element
              writer.WriteStartElement("Root");
              //Write Element in the root element
              writer.WriteElementString("PageSize", "33");
              writer.WriteElementString("Column", "Title");
              writer.WriteElementString("Column", "SalesPerson");
              writer.WriteElementString("Column", "SalesTarget");
              writer.WriteElementString("Column", "Achieved");
              writer.WriteElementString("Column", "UnitPrice");
              writer.WriteElementString("Column", "TotalRevenue");
              writer.WriteElementString("ListName", "SalesList");
              writer.WriteElementString("LibraryName", "Reports");
              //End the root element
              writer.WriteEndElement();
              //End the Xml Document
              writer.WriteEndDocument();
          }
  
  
  
      }
  </code></pre>
  
  <p>These are the methods I'm using to read from the XML file. These work totally fine with a webpart. But it gives an exception "Object reference not set for an instance of an object" when used in a timer job. </p>
  
  <pre><code>public class configuration : LayoutsPageBase
  {
     public List&lt;string&gt; readColumnName()
      {
          XmlReader reader = XmlReader.Create(Server.MapPath("~/AutomatedReport/ConfigFile.xml"));
          List&lt;string&gt; listColumnName = new List&lt;string&gt;();
          string element = "";
          while (reader.Read())
          {
              if (reader.NodeType == XmlNodeType.Element)
              {
                  element = reader.Name;
              }
  
              else if (reader.NodeType == XmlNodeType.Text)
              {
                  switch (element)
                  {
                      case "Column": listColumnName.Add(reader.Value.ToString());
                          break;
                  }
              }
          }
          return listColumnName;
      }
  </code></pre>
  
  <p>This is only one of the methods I'm using to read. This is reading the column names of a  list.</p>
  
  <p>The XML file is generated properly. Where am I going wrong here?</p>