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

发布于 2024-09-26 14:54:41 字数 1792 浏览 0 评论 0原文

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

我正在尝试制作小型控制台应用程序,它将写入 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;

          }

    }

}

请告诉我我犯了什么小错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-10-03 14:54:41

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文