I’m getting the name 'result' does not exist in the current context compilation error in the last few lines of my code.
Any idea what may be causing this?
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);
}
}
}
In your code snippet, you never instantiate the “result” variable. Your code miss the following statement: