Hello everyone .. I am new to c# technology..
I am trying to write an app which store some data in a xml file...
I am getting System.NullReferenceException ...
Below is the 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,FileShare.None);
            //xmldoc.Load(fs);
            //XML DOCUMENT SAVED
            xmldoc.Save(fs);
            fs.Close();

ny one have ny idea.. please help me...

While lines 5 and 6 create elements, they aren't automatically added to the xmldoc. From the documentation "Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods."

While lines 5 and 6 create elements, they aren't automatically added to the xmldoc. From the documentation "Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods."

hey thanx but can u elaborate more.. as i m totally new :)

Your line 3 creates an xmldocument, that's all good. Lines 5,6 create xmlelements but, even though you use the xmldocument that you created to create these, they are not a part of the xmldocument that you created. You need to add them to the xmldocument using one of the xmldocument insert methods. For example, AppendChild() should work:

xmldoc.AppendChild(res);
xmldoc.AppendChild(rsstitle);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.