gpt4 book ai didi

c# - C# 中的 LINQ to XML。枚举没有结果?

转载 作者:数据小太阳 更新时间:2023-10-29 02:21:39 40 4
gpt4 key购买 nike

我目前正在尝试从 Web 服务检索数据,例如,如果分数超过 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>

最佳答案

问题是 XML 文档指定了默认 namespace :

<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 语法有几个非典型特征:

关于c# - C# 中的 LINQ to XML。枚举没有结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46396929/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com