c# - C#编译错误: “The name does not exist in the curre

我在代码的最后几行中收到name 'result' does not exist in the current context编译错误。

知道是什么原因造成的吗?

class xmlreader
{
    public static void Main()
    {
        XDocument xdoc = XDocument.Load("file.xml");
        var lv1s = from lv1 in xdoc.Descendants("sitecollection")
                   select new
                   {
                       Header = lv1.Attribute("name").Value,
                       Children = lv1.Descendants("level2")
                   };
        foreach (var lv1 in lv1s)
        {
            result.AppendLine(lv1.Header);
            foreach (var lv2 in lv1.Children)
                result.AppendLine("   " + lv2.Attribute("name").Value);
        }

    }
}

最佳答案

在您的代码段中,您永远不会实例化“结果”变量。您的代码缺少以下语句:

var result=new System.Text.StringBuilder();

关于c# - C#编译错误: “The name does not exist in the current context” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8658320/

相关文章:

android - java.beans.VetoableChangeListener无法解析

compilation - javassist编译错误没有这样的类

gwt - 如何解决,Gwt编译错误: refers to missing type “classn

vb.net - 在VB.net故障中引发事件

makefile - 在 Fedora 17 中使用 'make' 命令编译时出错

c# - 为什么这一行代码与上面两行不同?

xml - 使用Perl写入xml时遇到问题

objective-c - Obj-C 中的乘法问题

opengl - glGetShaderiv()GL_COMPILE_STATUS返回GL_FALS

android - 编译android 4.0.3时找不到-ltinfo