0

I'm working on Windows Phone 8 C#/XAML .NET 4.5 Application

I'm trying to select element with a given name from an XML i'm getting, but when i try using Descendants to select it, it returns empty collection/list/array of results.

It returns fine when no name is selected, but when i try to search with name, it returns empty.

I'm probably just dumb and making a stupid mistake. Do you see some/can you explain?

EXAMPLES:

myXMLString:

<root>
  <element>
    <thisOne xmlns="something">example</thisOne>
  <element>
  <others></others>
</root>

code:

XDocument xmlData = XDocument.Parse(myXMLString);
//is always null
var thisOne = xmlData.Root.Descendants("thisOne").FirstOrDefault();
//returns the flattened version of the tree in a list
var descendants = xmlData.Root.Descedants().ToList();
mishan
  • 1,177
  • 3
  • 19
  • 42
  • The fact that the element you're trying to select has `xmlns="something"` (and that this was remarkable enough to include it in your question) should have been a big flashing indicator that there was something out of the ordinary about it and that it wasn't simply that `Descendants()` fails silently on Windows Phone 8. – JLRishe Dec 19 '14 at 16:09
  • I know that and thats why I'm asking. If you read the whole question, in the end I'm stating that I'm probably doing something wrong :) – mishan Dec 19 '14 at 16:11
  • Yeah, it's true that you said that. :) Sorry for being a bit snippy. – JLRishe Dec 19 '14 at 16:14
  • I should have probably selected less "attention seeking" title. – mishan Dec 19 '14 at 16:23

1 Answers1

1

You're currently looking for the elements called thisOne which aren't in a namespace. You need to specify the something namespace... the xmlns="something" part is specifying the default namespace for this element and further descendants. Fortunately, LINQ to XML makes it really easy to use XML namespaces:

XNamespace ns = "something";
var thisOne = xmlData.Root.Descendants(ns + "thisOne").FirstOrDefault();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for a quick answer. Is there a way to select this element without knowing the namespace beforehand (I'm getting an XML response from a badly written SOAP webservice that changes a lot and its WSDL is bad so i cannot add it as webservice in my project :) )? – mishan Dec 19 '14 at 16:10
  • 2
    @mishan Even if the SOAP webservice uses different _prefixes_ for its namespaces from one call to the next, it's unlikely that the namespace _URIs_ (the `something` portion in this example) would actually change. As long as the URIs are consistent, the approach above should work. – JLRishe Dec 19 '14 at 16:13
  • 1
    @mishan: I concur with what JLRishe said - but if you *really* wanted to only use the local name, you could use `xmlData.Root.Descendants().FirstOrDefault(x => x.LocalName == "thisOne")`. I would recommend using the namespace though. – Jon Skeet Dec 19 '14 at 17:43
  • I used the namespace in the end. Nice touch with the localname I knew about but forgot due to not using it :) – mishan Dec 23 '14 at 21:02