2017-09-25 90 views
2

我正在嘗試從Web服務檢索數據,如果分數超過90,我想對結果進行搜索。我試圖在不進行搜索的情況下帶回結果,並且沒有獲得任何結果。有人可以請我幫忙,我要去哪裏出錯?c#中的LINQ to XML。枚舉沒有結果?

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; 

這裏是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> 

回答

5

的問題是,XML文件指定一個默認命名空間:

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

所以,當你仰望你必須指定命名空間元素:

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

有一對夫婦的LINQ的非典型特徵,以XML語法: