-1

i am stuck with this small problem in my code.

I am trying to make small console application which will write into xml document. I have used xmldocument and xmlnode concept.

ERROR i am getting is;

*An object reference is required for the non-static field, method, or property 'Write_xml.Program.give_node(System.Xml.XmlDocument)' C:\Documents and Settings\Administrator\Desktop\Write_xml\Write_xml\Program.cs*

code is okay except 1 error. I am not able to resolve it ,i want somebody to check it and correct it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace Write_xml
{
    class Program
    {


        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            XmlDocument lets = new XmlDocument();
            string path = @"D:\XMLFile.xml";

            doc.Load(path);


            XmlNode Rootnode = doc.SelectSingleNode("Number");

            XmlNode TakenOde = give_node(doc);
            Rootnode.AppendChild(TakenOde);
            doc.Save(path);


        }


        public XmlNode give_node(XmlDocument lets)
        {
              // On this xmldoc we will perform XMLNODE operations
              // for creat new nods and append child nodes
              //XmlNode RootNode = xmldoc.CreateElement("Root");

              XmlNode PersonsNode = lets.CreateElement("Person");


              XmlNode NameNode = lets.CreateElement("Name");
              PersonsNode.AppendChild(NameNode);
              NameNode.InnerText = "1st";


              XmlNode AgeNode = lets.CreateElement("Age");
              PersonsNode.AppendChild(AgeNode);
              AgeNode.InnerText = "2nd";


              XmlNode CityNode = lets.CreateElement("City");
              PersonsNode.AppendChild(CityNode);
              CityNode.InnerText = "3rd";

              return PersonsNode;

          }

    }

}

please let me what small mistake i am doing.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
  • You appear to have us confused with the Psychic Hotline. Could you say what the error is, rather than making us guess? See http://tinyurl.com/so-hints for more hints about writing a good question. – Jon Skeet Oct 09 '10 at 17:20
  • i understand this is a bit silly but i am really confused . and by the way ERROR iam geting: ------- An object reference is required for the non-static field, method, or property 'Write_xml.Program.give_node(System.Xml.XmlDocument)' C:\Documents and Settings\Administrator\Desktop\Write_xml\Write_xml\Program.cs – Sangram Nandkhile Oct 09 '10 at 17:23
  • Sangram: you seem to have a `\`` at the end of the line where you set the `path` variable. Is that a typo in you question or your code as a whole? – Matt Ellen Oct 09 '10 at 17:27
  • @Sangram: If you don’t want to learn how to ask a good question, then we don’t want to answer your bad question. – Timwi Oct 09 '10 at 17:46
  • @ mat: ya i removed it.. thanx – Sangram Nandkhile Oct 09 '10 at 18:02
  • @Sangram: Did you really think that the question was better without telling us what the error message was and where you were getting it? If I thought you were going to be so rude about suggestions for how to improve your question, I might not have answered it... – Jon Skeet Oct 10 '10 at 07:36

1 Answers1

2

You're trying to call an instance method, but without specifying an instance.

The simplest fix for this is to make the give_node method static.

I haven't looked at the rest of the code to see whether it's okay or not, although give_node should be called GiveNode to follow .NET naming conventions.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194