locked
how to take inputs from a xml and json files and display output RRS feed

  • Question

  • User569149469 posted

    Hi,

    I have two input files xml and json and I want to display the o/p of the horses

    <?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>
    
    {
      "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"
            }
          }
        ]
      }
    }
    

    what ever may be the input form be it xml or json I want the horsenames with price in ascending order in a c# console application.

    Can any one guide how to do this.

    with price in ascending order.

    Thursday, August 23, 2018 8:40 PM

All replies

  • User475983607 posted

    Your question is a bit confusing.  Can you explain why the client does not know the format or type to expect?  Can you post the code that "gets" the unexpected format and type?

    Thursday, August 23, 2018 10:01 PM
  • User569149469 posted

    Hi,

    The client can get input from a web api which can give o/p  as xml or json.If that is the case how to do it.

    Friday, August 24, 2018 12:50 AM
  • User475983607 posted

    The client can get input from a web api which can give o/p  as xml or json.If that is the case how to do it.

    The Web API response is determined by the content-type header sent by the client.  It's possible that the Web API response has a set format but that is known upfront. Also the type returned is known.

    How is it possible that the client does not know the type or format of the response?  Can you provide an example the reproduces this problem?  

    Friday, August 24, 2018 10:14 AM
  • User569149469 posted

    Hi,

    Thanks for the reply.I have a condition where I need to get o/p from xml and json.I am not sure why there is condition such that but I have a situation .I will

    appreciate if any one provides solution to the problem.

    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");
    
                    }
    
                }
    
    
            }

    Friday, August 24, 2018 8:43 PM
  • User569149469 posted
    Hi,
    I want to get data from xml for horse name and price but not able to get can any one help me
    
     public void ConvertXml()
            {
                var xmlHorseDoc = XDocument.Load(@"C:\FeedData\Race1.xml");
                var xmlHorseDocNodes = from nodes in xmlHorseDoc.Descendants("race") select nodes;
                var xmlHorseName = xmlHorseDocNodes.Elements("horses").Elements("horse").ToArray();
                var xmHorsePrice = xmlHorseDocNodes.Elements("prices").Elements("price").Elements("horse").ToArray();
               // XElement xmlHorseElement = xmlHorseDoc.Element("race");
                //var xmlHorseName=from itemHorse in xmlHorseDocNodes.Des?cendants("horse") join xmlHorseDocNodes.Descendants("prices")
    
                //                 select new { HorseName=itemHorse.Attribute("name").Value.ToString(),HorsePrice=itemHorse.Descendants() };
    
              //  var xmlHorsePriceName = from c in xmlHorseName
                                      //   join o in xmHorsePrice
                                            //  on (int)c.Element("number") equals
                                              //   (int)o.Element("horses").Elements("horse").Attributes("number").Single()
                                 //  select new { horsename=c.Attribute("name").Value.ToString() }  ;
    
            }

    Saturday, August 25, 2018 8:57 PM
  • User475983607 posted

    It's practically impossible to achieve this requirement.  However, it super easy if you know the XML format or you have the schema.  Your requirement is like trying to guess the location of a door in a building that has not been constructed and is without blueprints.

    You could build a parser that looks for keywords then searches forward and back through a buffer but without the knowing the data structure there is not way to to be 100% sure that you have the correct data.

    What is really confusing is it seems that you already know the XML and JSON format because you posted it above and you if looking for XML or JSON as shown in the sample code.  So I'm confused...

    Saturday, August 25, 2018 11:38 PM