about exception while writing in a file by filestream in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ASHUTOSH TRIPAT
    New Member
    • Oct 2010
    • 4

    #1

    about exception while writing in a file by filestream in c#

    I am writing some content in xml which i m getting from a url.
    I am using filestream for this but i am getting "null reference exception " ny body have ny idea?? plz help me...
    urgent ..
    following is code ..

    Code:
    public void StoreFeedData(string title,Uri url)
            {
                XmlDocument xmldoc = new XmlDocument();
               
                XmlElement rss = xmldoc.CreateElement("rss");
                XmlElement rsstitle = xmldoc.CreateElement("title");
                rsstitle.InnerText = title;
                rss.AppendChild(rsstitle);
                XmlElement rsslink = xmldoc.CreateElement("link");
                rsslink.InnerText = url.ToString();
                rss.AppendChild(rsslink);
                xmldoc.DocumentElement.InsertAfter(rss, xmldoc.DocumentElement.LastChild);
    
                //INSTANCE OF FILE IS CREATED 
                FileStream fs = new FileStream(@"\\Program Files\\RssFeed\\feedlist.xml", FileMode.Append, FileAccess.Read);
                //xmldoc.Load(fs);
                //XML DOCUMENT SAVED
                xmldoc.Save(fs);
                fs.Close();
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Which line is generating the null reference exception?

    I can't help but wonder if it's line 15 in your example, is that path valid? Wouldn't you need a "C:" in front of that so it knows where to find "Program Files"?

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Line 15 is for sure a problem.

      @ denotes a verbatim string literal, meaning that nothing is escaped, including backslashes.

      That line must fail, because there is no such path as \\Program Files.

      Either remove the @, or better, remove the excess backslashes:
      Code:
      @"c:\Program Files\RssFeed\feedlist.xml"
      Also, include the drive letter.

      Comment

      • ASHUTOSH TRIPAT
        New Member
        • Oct 2010
        • 4

        #4
        hi curtis... thanx for reply. I tried ur option but still its not working. I tried to debug and its line 12 which is throwing exception.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Please be more specific. What error is thrown? The name and the message, please.

          Comment

          Working...