0

Trying to get the value for the "title" node from this XML -> http://feeds.feedburner.com/dotnetshoutout-published

I am using this code:

var d = XDocument.Load("http://feeds.feedburner.com/dotnetshoutout-published");
var node = d.Root.Descendants().Where(x => x.Name == "title").FirstOrDefault();

Always returns null. Making me crazy, any assistance is appreciated.

1 Answer 1

1

I guess you have an Xml Namespace on your elements.If so, your element name won't be just title, it will be namespace + title.Instead you should check the LocalName :

var node = d.Root.Descendants().Where(x => x.LocalName == "title").FirstOrDefault();

Or, you can look at the namespace of your elements and create an XNamespace and use it to fetch elements:

XNamespace ns = "yournamespace";
var node = d.Root.Descendants(ns + "title").FirstOrDefault();

You can read the documentation to find more info about how to deal with xml namespaces.

Sign up to request clarification or add additional context in comments.

2 Comments

Since the XML document in this case is an Atom feed, the namespace is http://www.w3.org/2005/Atom.
Awesome got it working like this; var d = XDocument.Load(url); var ns = d.Root.GetDefaultNamespace(); var node = d.Root.Descendants(ns + "title").FirstOrDefault(); string title = node != null ? node.Value : "";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.