如何从两个不同的文件xml和json获取输入并显示o / p

时间:2018-08-23 20:47:05

标签: c# .net

我有两个输入文件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;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;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>

JSON:

{
  "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:\..\FeedData\Race.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 个答案:

答案 0 :(得分:2)

1)为您的每个源创建模型类。对于xml,您可以尝试以下操作:https://xmltocsharp.azurewebsites.net对于json,可以尝试以下操作:http://json2csharp.com

2)将您的源反序列化为对象。

3)使用linq来获取所需信息的对象