Imsopov
Imsopov

Reputation: 155

LINQ to XML in c# . Enumeration yielded no results?

I'm currently trying to retrieve data from a web service, I would like to do search on results if the score is over 90 for example. I have tried to bring back results without doing searches also and getting no results. Could someone please give me a hand in where I'm going wrong?

FundNamesPayload xmlresponse = new FundNamesPayload();
xmlresponse = search.SearchByName("Australiansuper", "GUID-Here", "Y");

MemoryStream XmlStream = new MemoryStream();
StreamReader XmlReader = new StreamReader(XmlStream);
XmlSerializer Serializer = new XmlSerializer(typeof(FundNamesPayload));
Serializer.Serialize(XmlStream, xmlresponse);

XmlStream.Seek(0, System.IO.SeekOrigin.Begin);
var str = XElement.Parse(XmlReader.ReadToEnd());

var Matching = from data in str.Descendants("FundName")
                where(int)data.Element("Score") > 90
                select data;

Here is an example of the XML

<SuperFundNamesPayload xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://superfundlookup.gov.au">
<Request>
  <Guid>************</Guid>
  <Name>HOST Plus</Name>
  <ActiveFundsOnly>Y</ActiveFundsOnly>
</Request>
<Response>
  <DateTimeRetrieved>2017-09-25T12:20:40.8446457+10:00</DateTimeRetrieved>
  <MatchingFundNames>
    <NumberOfRecords>2</NumberOfRecords>
    <MatchingFundName>
    <ABN>
      <Value>68657495890</Value>
      <IdentifierStatus>Active</IdentifierStatus>
    </ABN>
    <FundName>
      <Name>THE TRUSTEE FOR HOST PLUS SUPERANNUATION FUND</Name>
      <NameType>Entity Name</NameType>
      <Score>94</Score>
      <NameStatus>Current</NameStatus>
    </FundName>
    <Location>
      <StateTerritoryCode>VIC</StateTerritoryCode>
      <Postcode>3000</Postcode>
    </Location>
  </MatchingFundName>
  <MatchingFundName>
    <ABN>
      <Value>80567702967</Value>
      <IdentifierStatus>Active</IdentifierStatus>
    </ABN>
    <FundName>
      <Name>The Trustee for HOIST HYDRAULICS VIC P L SUPER FUND</Name>
      <NameType>Entity Name</NameType>
      <Score>73</Score>
      <NameStatus>Current</NameStatus>
    </FundName>
    <Location>
      <StateTerritoryCode>VIC</StateTerritoryCode>
      <Postcode>3137</Postcode>
    </Location>
  </MatchingFundName>
</MatchingFundNames>
</Response>
</SuperFundNamesPayload>

Upvotes: 3

Views: 1498

Answers (2)

ivan
ivan

Reputation: 11

I had a similar issue because I have forgot to write the constructor (the 'var xmlresponse = new FundNamesPayload();' line). I've got no exception, no error, just an empty (null) result..

Upvotes: 1

Michael Liu
Michael Liu

Reputation: 55469

The problem is that the XML document specifies a default namespace:

<SuperFundNamesPayload ... xmlns="http://superfundlookup.gov.au">

So you must specify that namespace when you look up elements:

XNamespace ns = "http://superfundlookup.gov.au";
var Matching = from data in str.Descendants(ns + "FundName")
               where (int)data.Element(ns + "Score") > 90
               select data;

There a couple atypical features of the LINQ to XML syntax:

Upvotes: 7

Related Questions