XAttribute null即使它存在?

时间:2021-10-20 02:33:01

doc = GetSecureXDocument("https://www.predictit.org/api/marketdata/all");

doc = GetSecureXDocument(“https://www.predictit.org/api/marketdata/all”);

            List <MarketContract> contracts = doc.Root.Element("Markets").Elements("MarketData").Elements("Contracts").Elements("MarketContract").Select(x => new MarketContract
        {
            //ID = int.Parse((string)x.Attribute("ID")),
            URL = (string)x.Attribute("URL"),
            Name = (string)x.Attribute("Name"),
            LongName = (string)x.Attribute("LongName"),
            ShortName = (string)x.Attribute("ShortName"),
            TickerSymbol = (string)x.Attribute("TickerSymbol"),
            Status = (string)x.Attribute("Status"),
            /*BestBuyNoCostInCents = int.Parse((string)x.Attribute("BestBuyNoCost")),
            BestBuyYesCostInCents = int.Parse((string)x.Attribute("BestBuyYesCost")),
            BestSellNoCostInCents = int.Parse((string)x.Attribute("BestSellNoCost")),
            BestSellYesCostInCents = int.Parse((string)x.Attribute("BestSellYesCost"))*/
        }).ToList();

The commented out sections throw nullargumentexceptions, and when the values of URL, Name, LongName etc are printed, they're all zero.

注释掉的部分抛出nullargumentexceptions,当打印URL,Name,LongName等的值时,它们都是零。

Here's the value of 'x' in the lambda during a run:

这是运行期间lambda中'x'的值:

    <MarketContract>
  <ID>3414</ID>
  <DateEnd>N/A</DateEnd>
  <Image>https://az620379.vo.msecnd.net/images/Contracts/small_f6d2f26b-8055-45a3-872f-0796dc47e238.png</Image>
  <URL>https://www.predictit.org/Contract/3414/Will-a-Democratic-candidate-win-the-2017-Virginia-gubernatorial-race</URL>
  <Name>Democratic</Name>
  <LongName>Will a Democratic candidate win the 2017 Virginia gubernatorial race?</LongName>
  <ShortName>Democratic</ShortName>
  <TickerSymbol>DEM.VAGOV17</TickerSymbol>
  <Status>Open</Status>
  <LastTradePrice>0.75</LastTradePrice>
  <BestBuyYesCost>0.78</BestBuyYesCost>
  <BestBuyNoCost>0.26</BestBuyNoCost>
  <BestSellYesCost>0.74</BestSellYesCost>
  <BestSellNoCost>0.22</BestSellNoCost>
  <LastClosePrice>0.75</LastClosePrice>
</MarketContract>

This is my first time dealing with XML, and if anyone could help, that would be greatly appreciated!

这是我第一次处理XML,如果有人可以提供帮助,那将非常感谢!

1 个解决方案

#1


1  

Those are elements, not attributes.

这些是元素,而不是属性。

An attribute is a name="value" pair inside a start tag.

属性是开始标记内的name =“value”对。

#1


1  

Those are elements, not attributes.

这些是元素,而不是属性。

An attribute is a name="value" pair inside a start tag.

属性是开始标记内的name =“value”对。