"一个对象引用是所必需的非静态字段,方法或特性" [英] "An object reference is required for the non-static field, method, or property"
问题描述
我坚持我的代码这个小问题。
i am stuck with this small problem in my code.
我试图让这将写入XML文档小型控制台应用程序。
我已经使用XmlDocument的和的XMLNode概念
I am trying to make small console application which will write into xml document. I have used xmldocument and xmlnode concept.
错误我得到的;
*一个对象引用是必需的非静态字段,方法或属性Write_xml.Program.give_node(System.Xml.XmlDocument)'C:\Documents和Settings\Administrator\Desktop\Write_xml\Write_xml\ Program.cs的*
*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*
代码是好的,除1错误。我不是能够解决它,我希望有人来检查并纠正它。
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.
推荐答案
您正试图调用的实例的方法,但没有指定一个实例。
You're trying to call an instance method, but without specifying an instance.
这样做的最简单的解决方法是让 give_node
方法静态
。
The simplest fix for this is to make the give_node
method static
.
我没有看过的代码剩下的就看它是否是可以接受的,不是,虽然 give_node
应该叫 GiveNode
遵循.NET的命名规则。
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.
这篇关于"一个对象引用是所必需的非静态字段,方法或特性"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!