Justin Helgerson
Justin Helgerson

Reputation: 25551

Getting collection of elements from XML

I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
   <author>
      <name />
   </author>
   <title>Publishing Point Collection</title>
   <updated>2013-01-14T15:58:44.589Z</updated>
   <entry>
      <id>http://example.com:80/test filename.isml/settings</id>
      <title>Test Title</title>
      <updated>2013-01-14T15:47:15Z</updated>
      <link href="http://example.com:80/test filename.isml/settings" rel="related" type="application/atom+xml" title="Settings" />
      <link href="http://example.com:80/test filename.isml/state" rel="related" type="application/atom+xml" title="State" />
      <link href="http://example.com:80/test filename.isml/statistics" rel="related" type="application/atom+xml" title="Statistics" />
   </entry>
</feed>

I'm loading the document by doing:

//xmlContent is the XML shown above.
XDocument publishingDocument = XDocument.Parse(xmlContent);

I'm trying to get all of the link elements (specifically the href attribute, but, just the elements for now). But, anytime I specify an XName I never get any results. I've tried:

var data = publishingDocument.Elements("entry");
var data = publishingDocument.Elements("link").Attributes("href");
var data = publishingDocument.Root.Elements("entry");

None of these return any data. I must be missing something very simple.

Upvotes: 0

Views: 50

Answers (1)

Bala R
Bala R

Reputation: 109017

Try

XNamespace ns = "http://www.w3.org/2005/Atom";
var data = publishingDocument.Descendants(ns + "entry");

Upvotes: 2

Related Questions