“非静态字段,方法或属性需要对象引用”

我在我的代码中遇到了这个小问题。

我正在尝试制作可以写入xml文档的小型控制台应用程序。 我使用过xmldocument和xmlnode概念。

我得到的错误是;

*非静态字段,方法或属性’Write_xml.Program.give_node(System.Xml.XmlDocument)’C:\ Documents and Settings \ Administrator \ Desktop \ Write_xml \ Write_xml \ Program.cs *需要对象引用

代码没问题,除了1错误。 我无法解决它,我希望有人检查并纠正它。

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; } } } 

请告诉我我正在做的小错误。

您正在尝试调用实例方法,但未指定实例。

最简单的解决方法是使give_node方法保持static

我没有查看其余代码以查看它是否可以,尽管应该将GiveNode称为GiveNode以遵循.NET命名约定。