为什么我的 linq to xml 查询没有按设计捕获空值?

发布于 2024-12-28 09:10:58 字数 9794 浏览 0 评论 0原文

截至今天,我还是 linq to xml 的新手...我当前的查询在遇到空日期字段时会抛出错误,尽管我尝试在 select 语句中检查空值:(

var makeInfo = from s in doc.Descendants("quote")
               where (s.Element("LastTradeDate") != null) &&
               ((DateTime)s.Element("LastTradeDate") == DateTime.Parse("1/20/12"))
               select s.Attribute("symbol").Value;

如果有帮助,请使用 yql 股票市场数据feed,我希望返回给定交易日期的所有符号,非常自记录)。

编辑: null 是否表示缺少元素或没有值的标签?有问题的元素存在但没有价值。这是数据示例:

<quote symbol="A">
      <Ask />
      <AverageDailyVolume>3788190</AverageDailyVolume>
      <Bid />
      <AskRealtime>41.09</AskRealtime>
      <BidRealtime>40.30</BidRealtime>
      <BookValue>12.437</BookValue>
      <Change_PercentChange>-0.85 - -2.03%</Change_PercentChange>
      <Change>-0.85</Change>
      <Commission />
      <ChangeRealtime>-0.85</ChangeRealtime>
      <AfterHoursChangeRealtime>N/A - N/A</AfterHoursChangeRealtime>
      <DividendShare>0.00</DividendShare>
      <LastTradeDate>1/20/2012</LastTradeDate>
      <TradeDate />
      <EarningsShare>2.85</EarningsShare>
      <ErrorIndicationreturnedforsymbolchangedinvalid />
      <EPSEstimateCurrentYear>3.18</EPSEstimateCurrentYear>
      <EPSEstimateNextYear>3.47</EPSEstimateNextYear>
      <EPSEstimateNextQuarter>0.79</EPSEstimateNextQuarter>
      <DaysLow>40.68</DaysLow>
      <DaysHigh>41.90</DaysHigh>
      <YearLow>28.67</YearLow>
      <YearHigh>55.33</YearHigh>
      <HoldingsGainPercent>- - -</HoldingsGainPercent>
      <AnnualizedGain />
      <HoldingsGain />
      <HoldingsGainPercentRealtime>N/A - N/A</HoldingsGainPercentRealtime>
      <HoldingsGainRealtime />
      <MoreInfo>cnsprmiIed</MoreInfo>
      <OrderBookRealtime />
      <MarketCapitalization>14.273B</MarketCapitalization>
      <MarketCapRealtime />
      <EBITDA>1.326B</EBITDA>
      <ChangeFromYearLow>+12.33</ChangeFromYearLow>
      <PercentChangeFromYearLow>+43.01%</PercentChangeFromYearLow>
      <LastTradeRealtimeWithTime>N/A - &lt;b&gt;41.00&lt;/b&gt;</LastTradeRealtimeWithTime>
      <ChangePercentRealtime>N/A - -2.03%</ChangePercentRealtime>
      <ChangeFromYearHigh>-14.33</ChangeFromYearHigh>
      <PercebtChangeFromYearHigh>-25.90%</PercebtChangeFromYearHigh>
      <LastTradeWithTime>Jan 20 - &lt;b&gt;41.00&lt;/b&gt;</LastTradeWithTime>
      <LastTradePriceOnly>41.00</LastTradePriceOnly>
      <HighLimit />
      <LowLimit />
      <DaysRange>40.68 - 41.90</DaysRange>
      <DaysRangeRealtime>N/A - N/A</DaysRangeRealtime>
      <FiftydayMovingAverage>36.4185</FiftydayMovingAverage>
      <TwoHundreddayMovingAverage>37.0112</TwoHundreddayMovingAverage>
      <ChangeFromTwoHundreddayMovingAverage>+3.9888</ChangeFromTwoHundreddayMovingAverage>
      <PercentChangeFromTwoHundreddayMovingAverage>+10.78%</PercentChangeFromTwoHundreddayMovingAverage>
      <ChangeFromFiftydayMovingAverage>+4.5815</ChangeFromFiftydayMovingAverage>
      <PercentChangeFromFiftydayMovingAverage>+12.58%</PercentChangeFromFiftydayMovingAverage>
      <Name>Agilent Technolog</Name>
      <Notes />
      <Open>41.77</Open>
      <PreviousClose>41.85</PreviousClose>
      <PricePaid />
      <ChangeinPercent>-2.03%</ChangeinPercent>
      <PriceSales>2.20</PriceSales>
      <PriceBook>3.36</PriceBook>
      <ExDividendDate>1-Nov-06</ExDividendDate>
      <PERatio>14.68</PERatio>
      <DividendPayDate>Apr 25</DividendPayDate>
      <PERatioRealtime />
      <PEGRatio>0.85</PEGRatio>
      <PriceEPSEstimateCurrentYear>13.16</PriceEPSEstimateCurrentYear>
      <PriceEPSEstimateNextYear>12.06</PriceEPSEstimateNextYear>
      <Symbol>A</Symbol>
      <SharesOwned />
      <ShortRatio>1.00</ShortRatio>
      <LastTradeTime>4:00pm</LastTradeTime>
      <TickerTrend>&amp;nbsp;=====+&amp;nbsp;</TickerTrend>
      <OneyrTargetPrice>48.57</OneyrTargetPrice>
      <Volume>3906432</Volume>
      <HoldingsValue />
      <HoldingsValueRealtime />
      <YearRange>28.67 - 55.33</YearRange>
      <DaysValueChange>- - -2.03%</DaysValueChange>
      <DaysValueChangeRealtime>N/A - N/A</DaysValueChangeRealtime>
      <StockExchange>NYSE</StockExchange>
      <DividendYield />
      <PercentChange>-2.03%</PercentChange>
    </quote>
    <quote symbol="AA">
      <Ask />
      <AverageDailyVolume>30981100</AverageDailyVolume>
      <Bid />
      <AskRealtime>10.27</AskRealtime>
      <BidRealtime>9.15</BidRealtime>
      <BookValue>13.966</BookValue>
      <Change_PercentChange>-0.01 - -0.10%</Change_PercentChange>
      <Change>-0.01</Change>
      <Commission />
      <ChangeRealtime>-0.01</ChangeRealtime>
      <AfterHoursChangeRealtime>N/A - N/A</AfterHoursChangeRealtime>
      <DividendShare>0.12</DividendShare>
      <LastTradeDate>1/20/2012</LastTradeDate>
      <TradeDate />
      <EarningsShare>0.953</EarningsShare>
      <ErrorIndicationreturnedforsymbolchangedinvalid />
      <EPSEstimateCurrentYear>0.57</EPSEstimateCurrentYear>
      <EPSEstimateNextYear>0.96</EPSEstimateNextYear>
      <EPSEstimateNextQuarter>0.12</EPSEstimateNextQuarter>
      <DaysLow>10.06</DaysLow>
      <DaysHigh>10.2075</DaysHigh>
      <YearLow>8.45</YearLow>
      <YearHigh>18.47</YearHigh>
      <HoldingsGainPercent>- - -</HoldingsGainPercent>
      <AnnualizedGain />
      <HoldingsGain />
      <HoldingsGainPercentRealtime>N/A - N/A</HoldingsGainPercentRealtime>
      <HoldingsGainRealtime />
      <MoreInfo>cnsprmiIed</MoreInfo>
      <OrderBookRealtime />
      <MarketCapitalization>10.824B</MarketCapitalization>
      <MarketCapRealtime />
      <EBITDA>3.610B</EBITDA>
      <ChangeFromYearLow>+1.72</ChangeFromYearLow>
      <PercentChangeFromYearLow>+20.36%</PercentChangeFromYearLow>
      <LastTradeRealtimeWithTime>N/A - &lt;b&gt;10.17&lt;/b&gt;</LastTradeRealtimeWithTime>
      <ChangePercentRealtime>N/A - -0.10%</ChangePercentRealtime>
      <ChangeFromYearHigh>-8.30</ChangeFromYearHigh>
      <PercebtChangeFromYearHigh>-44.94%</PercebtChangeFromYearHigh>
      <LastTradeWithTime>Jan 20 - &lt;b&gt;10.17&lt;/b&gt;</LastTradeWithTime>
      <LastTradePriceOnly>10.17</LastTradePriceOnly>
      <HighLimit />
      <LowLimit />
      <DaysRange>10.06 - 10.2075</DaysRange>
      <DaysRangeRealtime>N/A - N/A</DaysRangeRealtime>
      <FiftydayMovingAverage>9.3315</FiftydayMovingAverage>
      <TwoHundreddayMovingAverage>11.206</TwoHundreddayMovingAverage>
      <ChangeFromTwoHundreddayMovingAverage>-1.036</ChangeFromTwoHundreddayMovingAverage>
      <PercentChangeFromTwoHundreddayMovingAverage>-9.25%</PercentChangeFromTwoHundreddayMovingAverage>
      <ChangeFromFiftydayMovingAverage>+0.8385</ChangeFromFiftydayMovingAverage>
      <PercentChangeFromFiftydayMovingAverage>+8.99%</PercentChangeFromFiftydayMovingAverage>
      <Name>Alcoa Inc. Common</Name>
      <Notes />
      <Open>10.14</Open>
      <PreviousClose>10.18</PreviousClose>
      <PricePaid />
      <ChangeinPercent>-0.10%</ChangeinPercent>
      <PriceSales>0.44</PriceSales>
      <PriceBook>0.73</PriceBook>
      <ExDividendDate>Nov  2</ExDividendDate>
      <PERatio>10.68</PERatio>
      <DividendPayDate>Nov 25</DividendPayDate>
      <PERatioRealtime />
      <PEGRatio>0.80</PEGRatio>
      <PriceEPSEstimateCurrentYear>17.86</PriceEPSEstimateCurrentYear>
      <PriceEPSEstimateNextYear>10.60</PriceEPSEstimateNextYear>
      <Symbol>AA</Symbol>
      <SharesOwned />
      <ShortRatio>2.30</ShortRatio>
      <LastTradeTime>4:00pm</LastTradeTime>
      <TickerTrend>&amp;nbsp;+==-=+&amp;nbsp;</TickerTrend>
      <OneyrTargetPrice>11.71</OneyrTargetPrice>
      <Volume>23356074</Volume>
      <HoldingsValue />
      <HoldingsValueRealtime />
      <YearRange>8.45 - 18.47</YearRange>
      <DaysValueChange>- - -0.10%</DaysValueChange>
      <DaysValueChangeRealtime>N/A - N/A</DaysValueChangeRealtime>
      <StockExchange>NYSE</StockExchange>
      <DividendYield>1.18</DividendYield>
      <PercentChange>-0.10%</PercentChange>
    </quote>

提前非常感谢..

I'm new to linq to xml as of today... my current query throws an error when it hits a null date field, although I tried checking for nulls in my select statement:

var makeInfo = from s in doc.Descendants("quote")
               where (s.Element("LastTradeDate") != null) &&
               ((DateTime)s.Element("LastTradeDate") == DateTime.Parse("1/20/12"))
               select s.Attribute("symbol").Value;

(if it helps this uses a yql stock market data feed, and I'm looking to return all symbols with the trading date given, pretty self- documenting).

Edit:
Does null represent a missing element, or a tag with no value? The offending element exists but with no value. Here's a sample of the data:

<quote symbol="A">
      <Ask />
      <AverageDailyVolume>3788190</AverageDailyVolume>
      <Bid />
      <AskRealtime>41.09</AskRealtime>
      <BidRealtime>40.30</BidRealtime>
      <BookValue>12.437</BookValue>
      <Change_PercentChange>-0.85 - -2.03%</Change_PercentChange>
      <Change>-0.85</Change>
      <Commission />
      <ChangeRealtime>-0.85</ChangeRealtime>
      <AfterHoursChangeRealtime>N/A - N/A</AfterHoursChangeRealtime>
      <DividendShare>0.00</DividendShare>
      <LastTradeDate>1/20/2012</LastTradeDate>
      <TradeDate />
      <EarningsShare>2.85</EarningsShare>
      <ErrorIndicationreturnedforsymbolchangedinvalid />
      <EPSEstimateCurrentYear>3.18</EPSEstimateCurrentYear>
      <EPSEstimateNextYear>3.47</EPSEstimateNextYear>
      <EPSEstimateNextQuarter>0.79</EPSEstimateNextQuarter>
      <DaysLow>40.68</DaysLow>
      <DaysHigh>41.90</DaysHigh>
      <YearLow>28.67</YearLow>
      <YearHigh>55.33</YearHigh>
      <HoldingsGainPercent>- - -</HoldingsGainPercent>
      <AnnualizedGain />
      <HoldingsGain />
      <HoldingsGainPercentRealtime>N/A - N/A</HoldingsGainPercentRealtime>
      <HoldingsGainRealtime />
      <MoreInfo>cnsprmiIed</MoreInfo>
      <OrderBookRealtime />
      <MarketCapitalization>14.273B</MarketCapitalization>
      <MarketCapRealtime />
      <EBITDA>1.326B</EBITDA>
      <ChangeFromYearLow>+12.33</ChangeFromYearLow>
      <PercentChangeFromYearLow>+43.01%</PercentChangeFromYearLow>
      <LastTradeRealtimeWithTime>N/A - <b>41.00</b></LastTradeRealtimeWithTime>
      <ChangePercentRealtime>N/A - -2.03%</ChangePercentRealtime>
      <ChangeFromYearHigh>-14.33</ChangeFromYearHigh>
      <PercebtChangeFromYearHigh>-25.90%</PercebtChangeFromYearHigh>
      <LastTradeWithTime>Jan 20 - <b>41.00</b></LastTradeWithTime>
      <LastTradePriceOnly>41.00</LastTradePriceOnly>
      <HighLimit />
      <LowLimit />
      <DaysRange>40.68 - 41.90</DaysRange>
      <DaysRangeRealtime>N/A - N/A</DaysRangeRealtime>
      <FiftydayMovingAverage>36.4185</FiftydayMovingAverage>
      <TwoHundreddayMovingAverage>37.0112</TwoHundreddayMovingAverage>
      <ChangeFromTwoHundreddayMovingAverage>+3.9888</ChangeFromTwoHundreddayMovingAverage>
      <PercentChangeFromTwoHundreddayMovingAverage>+10.78%</PercentChangeFromTwoHundreddayMovingAverage>
      <ChangeFromFiftydayMovingAverage>+4.5815</ChangeFromFiftydayMovingAverage>
      <PercentChangeFromFiftydayMovingAverage>+12.58%</PercentChangeFromFiftydayMovingAverage>
      <Name>Agilent Technolog</Name>
      <Notes />
      <Open>41.77</Open>
      <PreviousClose>41.85</PreviousClose>
      <PricePaid />
      <ChangeinPercent>-2.03%</ChangeinPercent>
      <PriceSales>2.20</PriceSales>
      <PriceBook>3.36</PriceBook>
      <ExDividendDate>1-Nov-06</ExDividendDate>
      <PERatio>14.68</PERatio>
      <DividendPayDate>Apr 25</DividendPayDate>
      <PERatioRealtime />
      <PEGRatio>0.85</PEGRatio>
      <PriceEPSEstimateCurrentYear>13.16</PriceEPSEstimateCurrentYear>
      <PriceEPSEstimateNextYear>12.06</PriceEPSEstimateNextYear>
      <Symbol>A</Symbol>
      <SharesOwned />
      <ShortRatio>1.00</ShortRatio>
      <LastTradeTime>4:00pm</LastTradeTime>
      <TickerTrend>&nbsp;=====+&nbsp;</TickerTrend>
      <OneyrTargetPrice>48.57</OneyrTargetPrice>
      <Volume>3906432</Volume>
      <HoldingsValue />
      <HoldingsValueRealtime />
      <YearRange>28.67 - 55.33</YearRange>
      <DaysValueChange>- - -2.03%</DaysValueChange>
      <DaysValueChangeRealtime>N/A - N/A</DaysValueChangeRealtime>
      <StockExchange>NYSE</StockExchange>
      <DividendYield />
      <PercentChange>-2.03%</PercentChange>
    </quote>
    <quote symbol="AA">
      <Ask />
      <AverageDailyVolume>30981100</AverageDailyVolume>
      <Bid />
      <AskRealtime>10.27</AskRealtime>
      <BidRealtime>9.15</BidRealtime>
      <BookValue>13.966</BookValue>
      <Change_PercentChange>-0.01 - -0.10%</Change_PercentChange>
      <Change>-0.01</Change>
      <Commission />
      <ChangeRealtime>-0.01</ChangeRealtime>
      <AfterHoursChangeRealtime>N/A - N/A</AfterHoursChangeRealtime>
      <DividendShare>0.12</DividendShare>
      <LastTradeDate>1/20/2012</LastTradeDate>
      <TradeDate />
      <EarningsShare>0.953</EarningsShare>
      <ErrorIndicationreturnedforsymbolchangedinvalid />
      <EPSEstimateCurrentYear>0.57</EPSEstimateCurrentYear>
      <EPSEstimateNextYear>0.96</EPSEstimateNextYear>
      <EPSEstimateNextQuarter>0.12</EPSEstimateNextQuarter>
      <DaysLow>10.06</DaysLow>
      <DaysHigh>10.2075</DaysHigh>
      <YearLow>8.45</YearLow>
      <YearHigh>18.47</YearHigh>
      <HoldingsGainPercent>- - -</HoldingsGainPercent>
      <AnnualizedGain />
      <HoldingsGain />
      <HoldingsGainPercentRealtime>N/A - N/A</HoldingsGainPercentRealtime>
      <HoldingsGainRealtime />
      <MoreInfo>cnsprmiIed</MoreInfo>
      <OrderBookRealtime />
      <MarketCapitalization>10.824B</MarketCapitalization>
      <MarketCapRealtime />
      <EBITDA>3.610B</EBITDA>
      <ChangeFromYearLow>+1.72</ChangeFromYearLow>
      <PercentChangeFromYearLow>+20.36%</PercentChangeFromYearLow>
      <LastTradeRealtimeWithTime>N/A - <b>10.17</b></LastTradeRealtimeWithTime>
      <ChangePercentRealtime>N/A - -0.10%</ChangePercentRealtime>
      <ChangeFromYearHigh>-8.30</ChangeFromYearHigh>
      <PercebtChangeFromYearHigh>-44.94%</PercebtChangeFromYearHigh>
      <LastTradeWithTime>Jan 20 - <b>10.17</b></LastTradeWithTime>
      <LastTradePriceOnly>10.17</LastTradePriceOnly>
      <HighLimit />
      <LowLimit />
      <DaysRange>10.06 - 10.2075</DaysRange>
      <DaysRangeRealtime>N/A - N/A</DaysRangeRealtime>
      <FiftydayMovingAverage>9.3315</FiftydayMovingAverage>
      <TwoHundreddayMovingAverage>11.206</TwoHundreddayMovingAverage>
      <ChangeFromTwoHundreddayMovingAverage>-1.036</ChangeFromTwoHundreddayMovingAverage>
      <PercentChangeFromTwoHundreddayMovingAverage>-9.25%</PercentChangeFromTwoHundreddayMovingAverage>
      <ChangeFromFiftydayMovingAverage>+0.8385</ChangeFromFiftydayMovingAverage>
      <PercentChangeFromFiftydayMovingAverage>+8.99%</PercentChangeFromFiftydayMovingAverage>
      <Name>Alcoa Inc. Common</Name>
      <Notes />
      <Open>10.14</Open>
      <PreviousClose>10.18</PreviousClose>
      <PricePaid />
      <ChangeinPercent>-0.10%</ChangeinPercent>
      <PriceSales>0.44</PriceSales>
      <PriceBook>0.73</PriceBook>
      <ExDividendDate>Nov  2</ExDividendDate>
      <PERatio>10.68</PERatio>
      <DividendPayDate>Nov 25</DividendPayDate>
      <PERatioRealtime />
      <PEGRatio>0.80</PEGRatio>
      <PriceEPSEstimateCurrentYear>17.86</PriceEPSEstimateCurrentYear>
      <PriceEPSEstimateNextYear>10.60</PriceEPSEstimateNextYear>
      <Symbol>AA</Symbol>
      <SharesOwned />
      <ShortRatio>2.30</ShortRatio>
      <LastTradeTime>4:00pm</LastTradeTime>
      <TickerTrend>&nbsp;+==-=+&nbsp;</TickerTrend>
      <OneyrTargetPrice>11.71</OneyrTargetPrice>
      <Volume>23356074</Volume>
      <HoldingsValue />
      <HoldingsValueRealtime />
      <YearRange>8.45 - 18.47</YearRange>
      <DaysValueChange>- - -0.10%</DaysValueChange>
      <DaysValueChangeRealtime>N/A - N/A</DaysValueChangeRealtime>
      <StockExchange>NYSE</StockExchange>
      <DividendYield>1.18</DividendYield>
      <PercentChange>-0.10%</PercentChange>
    </quote>

Many thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里的微风 2025-01-04 09:10:58

难道是缺少symbol属性吗?

var makeInfo = 
    from s in doc.Descendants("quote")
    where s.Element("LastTradeDate") != null
       && (DateTime)s.Element("LastTradeDate") == DateTime.Parse("1/20/12")
       && s.Attribute("symbol") != null
    select s.Attribute("symbol").Value;

编辑:如果您的日期始终遵循美国格式 (MM/dd/yy),您可以尝试:

CultureInfo enUS = new CultureInfo("en-US");
DateTime targetDate = new DateTime(2012, 1, 20);

var makeInfo = 
    from s in doc.Descendants("quote")
    where s.Element("LastTradeDate") != null
       && s.Attribute("symbol") != null
    let dateStr = s.Element("LastTradeDate").Value
    where !string.IsNullOrEmpty(dateStr)
       && DateTime.Parse(dateStr, enUS) == targetDate
    select s.Attribute("symbol").Value;

Could it be that the symbol attribute is missing?

var makeInfo = 
    from s in doc.Descendants("quote")
    where s.Element("LastTradeDate") != null
       && (DateTime)s.Element("LastTradeDate") == DateTime.Parse("1/20/12")
       && s.Attribute("symbol") != null
    select s.Attribute("symbol").Value;

Edit: If your dates will always follow the US format (MM/dd/yy), you could try:

CultureInfo enUS = new CultureInfo("en-US");
DateTime targetDate = new DateTime(2012, 1, 20);

var makeInfo = 
    from s in doc.Descendants("quote")
    where s.Element("LastTradeDate") != null
       && s.Attribute("symbol") != null
    let dateStr = s.Element("LastTradeDate").Value
    where !string.IsNullOrEmpty(dateStr)
       && DateTime.Parse(dateStr, enUS) == targetDate
    select s.Attribute("symbol").Value;
负佳期 2025-01-04 09:10:58

你缺少一些代码。试试这个:

var makeInfo = from s in doc.Descendants("quote")
               where (s.Element("LastTradeDate").Value != null) &&
               ((DateTime)s.Element("LastTradeDate").Value == DateTime.Parse("1/20/12"))
               select s.Attribute("symbol").Value;

You're missing a bit of code. Try this:

var makeInfo = from s in doc.Descendants("quote")
               where (s.Element("LastTradeDate").Value != null) &&
               ((DateTime)s.Element("LastTradeDate").Value == DateTime.Parse("1/20/12"))
               select s.Attribute("symbol").Value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文