Почему мой запрос linq to xml не улавливает пустые значения, как задумано? - PullRequest
1 голос
/ 22 января 2012

Я новичок в 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, и я надеюсь вернуть все символы с указанной торговой датой, довольно самодокументируемый).

Редактировать: Представляет ли 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>

Заранее большое спасибо ..

Ответы [ 2 ]

1 голос
/ 22 января 2012

Вам не хватает немного кода.Попробуйте это:

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;
1 голос
/ 22 января 2012

Может быть, отсутствует атрибут 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;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...