Basic LINQ : Why is this code returning null

  • Thread starter Thread starter FasterThanGravity
  • Start date Start date
F

FasterThanGravity

Guest
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using System.Xml.Linq;



namespace ConsoleApplication6
{

/// <summary>
/// //Create Save Load XML Doc
/// </summary>

class Program
{
static void Main(string[] args)
{
XDocument empoloyees =
new XDocument (
new XElement ("Employees",
new XElement ("Employee",
new XElement ("Name","John Smith"),
new XElement ("Phone", "12345678")),
new XElement ("Employee",
new XElement("Name","Jane Smith"),
new XElement("Phone","111123456")
)
));

empoloyees.Save("employee.xml");
var thedoc = XDocument.Load("employee.xml");

XElement root = thedoc.Element("Employees");
IEnumerable<XElement> employeeCount = root.Elements();

XElement justForParent = thedoc.Element("Name").Parent;
/*Why is the code above returning null? I am confused as I thought the parent node of "Name" would be Employee? And the parent node of that would be "Employees"? */

Console.WriteLine(justForParent);

/* foreach (var emp in employeeCount)
{
XElement nameNode = emp.Element("Name");
Console.WriteLine(nameNode.Value);

XElement phoneNode = emp.Element("Phone");
Console.WriteLine("{0}",phoneNode.Value);
Console.WriteLine("\n");
}*/


Console.ReadLine();


}
/*Methods:
* Nodes returns IEnumerable<object>
* Elements returns IEnumerable<XElement>
* Element returns XElement
* Parent returns XElement
* */
}
}



Thanks.

Continue reading...
 
Back
Top