2017-09-25 3 views
2

現在、Webサービスからデータを取得しようとしていますが、たとえばスコアが90を超える場合は検索結果を検索したいと考えています。私も検索をせずに結果を得ることなく結果を取り戻そうとしました。誰かが私が間違っている場所で私に手を差し伸べてもらえますか?LINQ to XML in C#。列挙は結果をもたらさなかったか?

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

あなたが見上げるときですから、その名前空間を指定する必要があります。ここでは

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文書がデフォルトの名前空間を指定することである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

の一例です要素:

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

は、カップルXML構文にLINQの非定型な特徴があります:

  • あなたはそのimplicit string conversion operator代わりnewのを使用してXNamespaceオブジェクトを作成します。
  • XNamespaceオブジェクトと文字列を+演算子で連結して、名前空間修飾名(XName)を作成します。
関連する問題