3

Hey all i have looked thoroughly through all the questions containing XDocument and while they are all giving an answer to what I'm looking for (mostly namespaces issues) it seems it just won't work for me.

The problem I'm having is that I'm unable to select any value, be it an attribute or element.

Using this XML

I'm trying to retrieve the speaker's fullname.

    public void GetEvent()
    {
        var xdocument = XDocument.Load(@"Shared\techdays2013.xml");
        XNamespace xmlns = "http://www.w3.org/2001/XMLSchema-instance";

        var data = from c in xdocument.Descendants(xmlns + "speaker")
                   select c.Element(xmlns + "fullname").Value;
    }
ctacke
  • 66,480
  • 18
  • 94
  • 155
Syneryx
  • 1,278
  • 11
  • 18
  • 3
    Are you sure you are querying the correct namespace? There is no default namespace for this doc (the namespace specified has a prefix but this isn't applied to the elements in the doc). Try querying without the xmlns prefix as I think the elements are probably in the `no namespace` namespace, if that makes sense! – Charleh Jan 29 '13 at 11:26
  • Related question [Xml.Linq: Descendants() returns nothing](http://stackoverflow.com/q/7503276) where namespace was required to specify. – Michael Freidgeim Sep 01 '16 at 06:13

3 Answers3

2

You can omit the namespace declaration in your linq statement.

public void GetEvent()
{
    var xdocument = XDocument.Load(@"Shared\techdays2013.xml");
    //XNamespace xmlns = "http://www.w3.org/2001/XMLSchema-instance";

    var data = from c in xdocument.Descendants("speaker")
               select c.Element("fullname").Value;
}
Jehof
  • 34,674
  • 10
  • 123
  • 155
  • Thank you for your quick response it turned out that besides the namespace thing, I was also assuming to view more of the result while debugging witch is not true and thus led to confusion. – Syneryx Jan 29 '13 at 15:40
1

1) You have to drop the namespace

2) You'll have to query more precisely. All your <speaker> elements inside <speakers> have a fullname but in the next section I spotted <speaker id="94" />

A simple fix (maybe not the best) :

//untested
var data = from c in xdocument.Root.Descendants("speakers").Descendants("speaker")
          select c.Element("fullname").Value;

You may want to specify the path more precise:

xdocument.Element("details").Element("tracks").Element("speakers").
H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    Thank you for your quick response it turned out that besides the namespace thing, I was also assuming to view more of the result while debugging witch is not true and thus led to confusion. – Syneryx Jan 29 '13 at 15:41
  • @Syneryx I had to do a lot of searching, but your comment turned out to be my problem. I was trying to do lcOutputDocument.Element("Item").Elements() in the immediate window and this returned null, yet my code worked as intended when just being run normally. – Chucky Apr 20 '15 at 13:26
1

You can omit WebClient because you have direct local access to a file. I'm just showing a way to process your file on my machine.

void Main()
{
    string p = @"http://events.feed.comportal.be/agenda.aspx?event=TechDays&year=2013&speakerlist=c%7CExperts";
    using (var client = new WebClient())
    {
        string str = client.DownloadString(p);
        var xml = XDocument.Parse(str);

        var result = xml.Descendants("speaker")
                        .Select(speaker => GetNameOrDefault(speaker));


        //LinqPad specific call        
        result.Dump();
    }
}
public static string GetNameOrDefault(XElement element)
{
    var name = element.Element("fullname");
    return name != null ? name.Value : "no name";
}

prints:

Bart De Smet 
Daniel Pearson 
Scott Schnoll 
Ilse Van Criekinge 
John Craddock 
Corey Hynes 
Bryon Surace 
Jeff Prosise 
Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
  • Thank you for your quick response it turned out that besides the namespace thing, I was also assuming to view more of the result while debugging witch is not true and thus led to confusion. – Syneryx Jan 29 '13 at 15:41