[英]“An object reference is required for the non-static field, method, or property”
我在我的代码中遇到了这个小问题。
我正在尝试制作将写入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
。
我没有看代码的其余部分以查看它是否还可以,尽管give_node
应该被称为GiveNode
以遵循.NET命名约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.