我在代码的最后几行中收到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/