static void Main() { var context = new FootballEntities(); var data = context.Leagues.OrderBy(l => l.LeagueName).Select(l => new { leagueName = l.LeagueName, teams = l.Teams.OrderBy(t => t.TeamName).Select(t => t.TeamName) }); JsonSerializer output = new JsonSerializer(); output.Formatting = Formatting.Indented; output.NullValueHandling = NullValueHandling.Include; using (StreamWriter sw = new StreamWriter(@"../../leagues-and-teams.json")) using (JsonWriter writer = new JsonTextWriter(sw)) { foreach (var entity in data) { output.Serialize(writer, entity); } } Console.WriteLine("Successfully created JSON file!"); }
static void Main() { var context = new FootballEntities(); XmlDocument doc = new XmlDocument(); doc.Load("../../leagues-and-teams.xml"); XmlNode Root = doc.DocumentElement; var leagueNumb = 0; foreach (XmlNode node in Root.ChildNodes) { leagueNumb++; Console.WriteLine("Processing league #{0} ...",leagueNumb); string leagueName = ""; if (node.FirstChild != null && node.FirstChild.Name == "league-name" && !context.Leagues.Any(l => l.LeagueName == node.FirstChild.InnerText.ToString())) { context.Leagues.Add(new League { LeagueName = node.FirstChild.InnerText }); leagueName = node.FirstChild.InnerText; Console.WriteLine("Created league: {0}", node.FirstChild.InnerText); context.SaveChanges(); } else { if (node.FirstChild != null && node.FirstChild.InnerText != "") { Console.WriteLine("Existing league: {0}", node.FirstChild.InnerText); context.SaveChanges(); } } if (node["teams"] != null && node["teams"].HasChildNodes) { foreach (XmlNode team in node["teams"].ChildNodes) { if (team.Attributes["name"].Value != null) { var name = team.Attributes["name"].Value; var country = (team.Attributes["country"] != null) ? team.Attributes["country"].Value : ""; if (country != "" && !context.Teams.Any(t => t.TeamName == name && t.Country.CountryName == country)) { var addTeam = context.Teams.Add(new Team { TeamName = name, CountryCode = context.Countries .First(c => c.CountryName == country).CountryCode }); if (leagueName != "" && addTeam.Leagues.Count == 0) { addTeam.Leagues.Add(context.Leagues .FirstOrDefault(l => l.LeagueName == leagueName)); Console.WriteLine("Added team to league: {0} to {1}",addTeam.TeamName,leagueName); } Console.WriteLine("Created team: {0} ({1})", addTeam.TeamName, addTeam.Country.CountryName ?? "no country"); } else if (country == "") { var addTeam = context.Teams.Add(new Team { TeamName = name }); if (leagueName != "") { addTeam.Leagues.Add(context.Leagues .FirstOrDefault(l => l.LeagueName == leagueName)); Console.WriteLine("Added team to league: {0} to {1}", addTeam.TeamName, leagueName); } Console.WriteLine("Created team: {0} ({1})", addTeam.TeamName, "no country"); } else { Console.WriteLine("Existing team: {0} ({1})", team.Attributes["name"].Value, (country == "") ? "no country" : country); } } } } } context.SaveChanges(); }
static void Main() { var context = new FootballEntities(); var data = context.InternationalMatches .Select(im => new { im.HomeCountryCode, im.AwayCountryCode, HomeCountry = im.Country1.CountryName, AwayCountry = im.Country.CountryName, im.MatchDate, im.League.LeagueName, im.HomeGoals, im.AwayGoals }) .OrderBy(im => im.MatchDate) .ThenBy(im => im.HomeCountry) .ThenBy(im => im.AwayCountry); //Order the matches by date (from the earliest) and by home country and //away country alphabetically as second and third criteria. var doc = new XmlDocument(); var xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null); var root = doc.DocumentElement; doc.InsertBefore(xmlDeclaration, root); var matches = doc.CreateElement(string.Empty, "matches", string.Empty); foreach (var entity in data) { XmlNode match = doc.CreateElement("match"); if (entity.MatchDate != null) { if (entity.MatchDate.Value.TimeOfDay.TotalSeconds > 0) { var date = doc.CreateAttribute("date-time"); date.Value = String.Format("{0:MM-dd-yyyy hh:mm}", entity.MatchDate); match.Attributes.Append(date); } else { var date = doc.CreateAttribute("date"); date.Value = String.Format("{0:MM-dd-yyyy}", entity.MatchDate); match.Attributes.Append(date); } } XmlNode homeCountry = doc.CreateElement("home-country"); var homeCountryCode = doc.CreateAttribute("code"); homeCountryCode.Value = entity.HomeCountryCode; homeCountry.Attributes.Append(homeCountryCode); homeCountry.InnerText = entity.HomeCountry; match.AppendChild(homeCountry); XmlNode awayCountry = doc.CreateElement("away-country"); var awayCountryCode = doc.CreateAttribute("code"); awayCountryCode.Value = entity.AwayCountryCode; awayCountry.Attributes.Append(awayCountryCode); awayCountry.InnerText = entity.AwayCountry; match.AppendChild(awayCountry); if (entity.HomeGoals != null && entity.AwayGoals != null) { XmlNode goals = doc.CreateElement("goals"); goals.InnerText = entity.HomeGoals + "-" + entity.AwayGoals; match.AppendChild(goals); } if (entity.LeagueName != null) { XmlNode league = doc.CreateElement("leauge"); league.InnerText = entity.LeagueName; match.AppendChild(league); } matches.AppendChild(match); } doc.AppendChild(matches); doc.Save("../../international-matches.xml"); Console.WriteLine("XML creation completed!"); }