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();
注释掉的部分抛出nullargumentexceptions,当打印URL,Name,LongName等的值时,它们全部为零。
这里&#39; x&#39;的价值在跑步期间在lambda中:
<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>
这是我第一次处理XML,如果有人可以提供帮助,那将非常感谢!
答案 0 :(得分:1)
这些是元素,而不是属性。
属性是开始标记内的name="value"
对。