Hope that helps In your first example doc.Root already gives you the element. There is no element under the root with that name so doc.Root.Element("graphml") will return null in that case. To complete your first example it should look like this: Notice the XNamespace addition.
var graphml = doc.Root;
var ns = (XNamespace)"http://graphml.graphdrawing.org/xmlns";
foreach (var keyElement in graphml.Elements(ns + "key"))
var xNameKey = XName.Get("key", doc.Root.GetDefaultNamespace().NamespaceName);
foreach (var keyElement in doc.Root.Elements(xNameKey))
like below fixes the issue First of all xNode.SelectSingleNode("ShowImdb") doesn't exist in your doc which is causing NullReferenceException. If you want to persist changes to xDucument you'll have to save it before closing your application (releasing XDocument object). Use XDocument.Save(string) for it. UPDATE:
How can i get a collection of child elements - query returns null using Xdocument
should help you out Can anybody please tell me why this Xdocument query is returning null when there are elements / attributes that i'm trying to grab. , That's because you are missing the model element.