public Bill LoadFavoriteBill(int id) { Bill favBill; using (var connection = new SqliteConnection (connectionString)) { using (var cmd = connection.CreateCommand ()) { connection.Open (); cmd.CommandText = "SELECT * FROM FavoriteBills WHERE id = @id"; var idParam = new SqliteParameter ("@id", id); cmd.Parameters.Add (idParam); using (var reader = cmd.ExecuteReader ()) { reader.Read (); favBill = new Bill { Id = Convert.ToInt32 (reader ["id"]), Title = (string)reader ["title"], ThomasLink = (string)reader ["thomas_link"], Notes = reader["notes"] == DBNull.Value ? "" : (string)reader["notes"] }; } } } return favBill; }
public static async Task<Bill> GetBillAsync (int id) { try { using (var httpClient = new HttpClient ()) { string url = String.Format ("https://www.govtrack.us/api/v2/bill/{0}?format=xml", id); var response = await httpClient.GetAsync (url); var stream = await response.Content.ReadAsStreamAsync (); var bill = LoadBill(stream, id); return bill; } } catch (Exception) { var bill = new Bill { Id = -1, Title = "Could not connect to the internet" }; return bill; } }
public void SaveFavoriteBill(Bill bill) { using (var connection = new SqliteConnection (connectionString)) { using (var cmd = connection.CreateCommand ()) { connection.Open (); string sql = "INSERT OR REPLACE Into FavoriteBills (id, title, thomas_link) Values (@id, @title, @thomas_link)"; var idParam = new SqliteParameter ("@id", bill.Id); var titleParam = new SqliteParameter ("@title", bill.Title); var thomas_linkParam = new SqliteParameter ("@thomas_link", bill.ThomasLink); cmd.Parameters.Add (idParam); cmd.Parameters.Add (titleParam); cmd.Parameters.Add (thomas_linkParam); cmd.CommandText = sql; cmd.ExecuteNonQuery (); } } }
static Bill LoadBill (Stream stream, int id) { XDocument billXml = XDocument.Load (stream); var bill = new Bill { Title = (string)(from title in billXml.Descendants ("title_without_number") select title).First (), ThomasLink = (string)(from link in billXml.Descendants ("thomas_link") select link).First (), Id = id }; return bill; }