我目前正在尝试从网络服务中检索数据,如果分数超过90,我想搜索结果。我试图在不进行搜索的情况下恢复结果并且没有结果。有人可以帮我问一下我出错的地方吗?
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>
答案 0 :(得分: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 to XML语法有一些非典型特性:
new
创建implicit string conversion operator个对象。+
运算符的字符串连接来创建名称空间限定名称(XName)。答案 1 :(得分:1)
我有一个类似的问题,因为我忘记编写构造函数(“ var xmlresponse = new FundNamesPayload();”行)。我没有例外,没有错误,只是一个空(空)结果。