【问题标题】:LINQ to XML in c# . Enumeration yielded no results?c# 中的 LINQ to XML 。枚举没有结果?
【发布时间】:2017-09-25 02:48:11
【问题描述】:

我目前正在尝试从 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>

【问题讨论】:

    标签: c# xml linq


    【解决方案1】:

    问题在于 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 语法有几个非典型特性:

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,因为我忘记编写构造函数('var xmlresponse = new FundNamesPayload();' 行)。我没有例外,没有错误,只是一个空(null)结果..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多