LINQ to XML cannot get element

I'm trying to parse a tiny xml return from a web service using LINQ to XML. XML looks like this:

<ns:ResponseTest xmlns:ns="http://websvc.tst.com">
    <ns:return>true</ns:return>
</ns:ResponseTest>

      

And looking around the net I found this, which should read in the first value with the given name:

var returnValue = XDocument.Parse(xml).Descendants().FirstOrDefault(n => n.Name == "return");

      

But it always appears as null. I also tried to use a namespace in the name (when I put a name over it (above: "return"), it tells me that I can use the name {namespace} to provide a namespace), so it was "{ns} return" ... However, this also didn't return anything.

How do I get the return value from the above xml?

EDIT: I also tried the solution here Reading data from XML and the same thing happened. I could not find it to find the specified items.

+3


source to share


2 answers


Try the following:



XNamespace ns = "http://websvc.tst.com";
var returnValue = XDocument.Parse(xml).Descendants(ns + "return").FirstOrDefault();

      

+4


source


You can use LocalName to get the unqualified part of the name



var returnValue = XDocument.Parse(xml).Descendants()
                           .FirstOrDefault(n => n.Name.LocalName == "return");

      

+3


source







All Articles