Linq to xml not working

Aikencura used Ask the Experts™

Can somebody explain me why my code isn't working?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Data;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Linq;
using System.Diagnostics;

namespace TelSearchModule
    public partial class _Default : System.Web.UI.Page


        protected void Page_Load(object sender, EventArgs e)
            ///Start Test
            // Load XML doc
            XDocument rssFeed = XDocument.Load("");
            // Define XML Namespace
            XNamespace Atom = "";
            XNamespace OpenSearch = "";
            XNamespace tel = "";

            var entries = from entry in rssFeed.Descendants("entry")
                          select new
                              Title = entry.Element("title").Value,
                              FirstName = entry.Element("tel:FirstName").Value

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

If the elements are in a different namespace, then you need to include those in the XDocument query.


planetsDoc = XDocument.Load("../../Planets.xml");
stacky.DataContext = planetsDoc.Element("{http://planetsNS}SolarSystemPlanets").Elements();


I don't understand, can you explain in "noob" language please..with my code.
If you test your suggestion, you get:
{"The '{' character, hexadecimal value 0x7B, cannot be included in a name."}
Most Valuable Expert 2012
Top Expert 2008
There are different ways to build the query:

   Without the XNamespace reference:

            var entries = from entry in rssFeed.Descendants("{}entry")
                          select entry;


   With the XNamespace reference:

              var entries = from entry in rssFeed.Descendants(Atom + "entry")
                          select new
                              Title = (string)entry.Element(Atom + "title"),
                              FirstName = (string)entry.Element(tel + "firstname")

Also, XML is case sensitive, so you need "firstname" and not "FirstName".

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial