我有两个输入文件 xml 和 json,我想显示马的 o/p
.XML:
<?xml version="1.0"?>
<meeting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<date>16/12/2017</date>
<MeetingType>HorseRacing</MeetingType>
<track name="field" TranslatedName="field" club="R.C." location="M" country="US" state="IC" condition="Good3"/>
<Meetingid>217</Meetingid>
<races>
<race number="1" name="Plate" id="114" Status="OPEN">
<NumberOfRunners>13</NumberOfRunners>
<start_time>16/12/2017 11:30:00 AM</start_time>
<distance metres="1100"/>
<horses>
<horse name="Advancing" country="US" age="2" sex="C" colour="b" foaling_date="01/08/2015" id="99">
<number>1</number>
<trainer name="Price" id="495">
<statistics>
<statistic type="one_year" total="738" firsts="113" seconds="93" thirds="92"/>
</statistics>
</trainer>
<training_location>field</training_location>
<owners>New (Mgr:Murphy), Racing Pty Ltd (Mgr: Teo), Horse Ventures (Mgr:blom) &amp; Farm Llc (Mgr: J Mul)</owners>
<colours>Red, Yellow Stars And Halved Sleeves, Red Cap With White Newgate Logo</colours>
<current_blinker_ind>N</current_blinker_ind>
<prizemoney_won>0</prizemoney_won>
<jockey name="M J Dee" id="15233">
<statistics>
<statistic type="one_year" total="717" firsts="71" seconds="64" thirds="80"/>
</statistics>
</jockey>
<barrier>7</barrier>
<weight allocated="57" total="57"/>
</horse>
<horse name="Corl" country="US" age="2" sex="C" colour="b" foaling_date="22/08/2015" id="42">
<number>2</number>
<trainer name="Ryan" id="371">
<statistics>
<statistic type="one_year" total="66" firsts="7" seconds="10" thirds="11"/>
</statistics>
</trainer>
<training_location>field</training_location>
<owners> Park (Mgr: Douglas) &amp; T Dav</owners>
<colours>Royal Blue, Light Blue Hoops, White Cap</colours>
<current_blinker_ind>Y</current_blinker_ind>
<prizemoney_won>3000</prizemoney_won>
<last_four_starts>05</last_four_starts>
<last_ten_starts>05</last_ten_starts>
<jockey name="Maloney" id="86">
<statistics>
<statistic type="one_year" total="488" firsts="70" seconds="61" thirds="74"/>
</statistics>
</jockey>
<barrier>6</barrier>
<weight allocated="57" total="57"/>
</horse>
</horses>
<prices>
<price>
<priceType>WinFixedOdds</priceType>
<horses>
<horse number="1" Price="4.2"/>
<horse number="2" Price="12"/>
</horses>
</price>
</prices>
</race>
</races>
</meeting>
杰森:
{
"FixtureId": "bphVf_Ik6LkkxYT5aN1MgQvcta0",
"Timestamp": "2017-12-13T06:05:23Z",
"RawData": {
"FixtureName": "13:45 @hampton",
"Id": "bphVf_Ik6LkkxYT5aN1MgQvcta0",
"StartTime": "2017-12-13T13:45:00Z",
"Sequence": 1,
"Tags": {
"CourseType": "Flat",
"Distance": "0m 5f 21y",
"Going": "Fast",
"Runners": "9",
"MeetingCode": "94209",
"TrackCode": "hampton",
"Sport": "HorseRacing"
},
"Markets": [
{
"Id": "NbSeMfzhDCHT_HdtAYZF_7zjFkI",
"Selections": [
{
"Id": "b0Ut0-KyBdfknmjckAQHY1sxs3U",
"Price": 10.0,
"Tags": {
"participant": "1",
"name": "Toolatetodelegate"
}
},
{
"Id": "2GQLldt5t2lZCW93EgdBMwDIbP0",
"Price": 4.4,
"Tags": {
"participant": "2",
"name": "khaar"
}
}
],
"Tags": {
"Places": "3",
"type": "winner"
}
}
],
"Participants": [
{
"Id": 1,
"Name": "Toolatetodelegate",
"Tags": {
"Weight": "9st 7lbs",
"Drawn": "8",
"Jockey": "Carver",
"Number": "1",
"Trainer": "Barr"
}
},
{
"Id": 2,
"Name": "khaar",
"Tags": {
"Weight": "9st 6lbs",
"Drawn": "3",
"Jockey": "Eaves",
"Number": "2",
"Trainer": "Ryan"
}
}
]
}
}
无论输入形式是什么,无论是 xml 还是 json,我都希望在 c# 控制台应用程序中按升序排列价格的马名。
任何人都可以指导如何做到这一点吗?
随着价格的升序,无论输入文件是什么,我们都应该按升序显示马名和价格。
我正在使用这样的代码
public static void convertJson()
{
Dictionary<string, int> horseDic = new Dictionary<string, int>();
string jsonData = File.ReadAllText(@"C:..FeedDataRace.json");
dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(jsonData);
foreach (var item in jsonObj)
{
var itemName = item.Name;
var itemValue = item.Value;
if(itemName== "RawData")
{
var itemMar= item.Value.Find("Markets");
}
}
}
但我不知道这是正确的方法。
1(为每个源创建模型类。对于 xml,您可以尝试以下方法: https://xmltocsharp.azurewebsites.net 对于 json 这个: http://json2csharp.com
2( 将源代码反序列化为对象。
3( 使用 linq to 对象派生您需要的信息