static public List <Commande> ImporterXmlAvecLINQ() { List <Commande> lstCommandes = new List <Commande>(); XDocument doc = XDocument.Load("../../ListeCommandes.xml"); foreach (var c in doc.Descendants("Commande")) { Commande com = new Commande() { LstLignesCommandes = new List <LigneCommande>() }; com.IdCommande = int.Parse(c.Attribute("IdCommande").Value); com.IdClient = c.Attribute("IdClient").Value; com.Date = DateTime.Parse(c.Element("Date").Value); foreach (var lc in c.Descendants("LigneCommande")) { LigneCommande ligne = new LigneCommande(); ligne.IdProduit = int.Parse(lc.Attribute("IdProduit").Value); ligne.Quantité = int.Parse(lc.Attribute("Quantité").Value); // Nécessaire si CultureInfo = "fr-FR" ligne.PrixUnitaire = decimal.Parse(lc.Attribute("PrixUnitaire").Value.Replace('.', ',')); ligne.Réduction = float.Parse(lc.Attribute("Réduction").Value.Replace('.', ',')); com.LstLignesCommandes.Add(ligne); } lstCommandes.Add(com); } return(lstCommandes); }
/////////////////////////////////////////////////////////////////////////////////////////// //5.5 Désérialisation avec Linq To XML ///////////////////////////////////////////////////////////////////////////////////////////// public static List <Commande> ImporterXmlLinq() { List <Commande> listCom = new List <Commande>(); // Chargement du fichier xml XDocument doc = XDocument.Load(@"D:\ycappelle\winforms\ListCommande_serializer.xml"); var com = doc.Descendants("Commande"); foreach (XElement c in com) { var cde = new Commande() { listLigneCom = new List <LigneCommande>() }; cde.CommandeId = int.Parse(c.Attribute("CommandeId").Value); cde.DateCommande = DateTime.Parse(c.Attribute("DateCommande").Value); cde.DateLivraion = DateTime.Parse(c.Attribute("DateLivraion").Value); cde.NbProduitsCom = int.Parse(c.Attribute("NbProduitsCom").Value); cde.MontantCom = int.Parse(c.Attribute("MontantCom").Value); cde.Frais = int.Parse(c.Attribute("Frais").Value); cde.CustomerID = (c.Attribute("CustomerID").Value); var lignCom = c.Descendants("LigneCommande"); foreach (XElement ligne in lignCom) { var li = new LigneCommande(); li.ProductId = int.Parse(ligne.Attribute("ProductId").Value); li.UnitPrice = decimal.Parse(ligne.Attribute("UnitPrice").Value.Replace('.', ',')); li.Quantity = short.Parse(ligne.Attribute("Quantity").Value); li.Discount = float.Parse(ligne.Attribute("Discount").Value.Replace('.', ',')); cde.listLigneCom.Add(li); } listCom.Add(cde); } return(listCom); }
/// <summary> /// Remplit la liste de commande passé en paramètre avec le reader. /// </summary> /// <param name="listCommande">Liste de commandes à remplir.</param> /// <param name="reader">Le reader de base de donnée.</param> static private void GetListCommandeFromReader(BindingList <Commande> listCommande, SqlDataReader reader) { while (reader.Read()) { int iDCommande = (int)reader["OrderID"]; Commande commande = null; #region On regarde dans quelle commande ajouter la prochaine ligne de commande if (listCommande.Count == 0 || listCommande[listCommande.Count - 1].IDCommande != iDCommande) { commande = new Commande(); commande.IDCommande = (int)reader["OrderID"]; if (reader["CustomerID"] != DBNull.Value) { commande.IDClient = (string)reader["CustomerID"]; } if (reader["OrderDate"] != DBNull.Value) { commande.DateCommande = (DateTime)reader["OrderDate"]; } commande.LigneCommande = new List <LigneCommande>(); listCommande.Add(commande); } else { commande = listCommande[listCommande.Count - 1]; } #endregion #region On ajoute la ligne de commande LigneCommande lc = new LigneCommande(); lc.IDProduit = (int)reader["ProductID"]; lc.PrixUnitaire = (decimal)reader["UnitPrice"]; lc.Quantité = (short)reader["Quantity"]; lc.Réduction = (float)reader["Discount"]; commande.LigneCommande.Add(lc); #endregion } }
private static void GetListCommandeFromDataReader(List <Commande> listCom, SqlDataReader reader) { int comId = (int)reader["OrderID"]; if (listCom.Count == 0 || listCom[listCom.Count - 1].CommandeId != comId) { Commande com = new Commande() { listLigneCom = new List <LigneCommande>() }; com.CommandeId = (int)reader["OrderID"]; if (reader["CustomerID"] != DBNull.Value) { com.CustomerID = reader["CustomerID"].ToString(); } if (reader["OrderDate"] != DBNull.Value) { com.DateCommande = (DateTime)reader["OrderDate"]; } listCom.Add(com); } LigneCommande liCom = new LigneCommande(); liCom.ProductId = (int)reader["ProductID"]; liCom.UnitPrice = (decimal)reader["UnitPrice"]; liCom.Quantity = (short)reader["Quantity"]; liCom.Discount = (float)reader["Discount"]; listCom[listCom.Count - 1].listLigneCom.Add(liCom); }
private static void RécupérerCommandesDepuisSqlDataReader(SqlDataReader reader, List <Commande> lstCommandes) { while (reader.Read()) { int currentIdCommande = (int)reader["OrderID"]; if (lstCommandes.Count == 0 || currentIdCommande != lstCommandes.Last().IdCommande) { var com = new Commande() { LstLignesCommandes = new List <LigneCommande>() }; com.IdCommande = currentIdCommande; if (reader["CustomerID"] != DBNull.Value) { com.IdClient = reader["CustomerID"].ToString(); } if (reader["OrderDate"] != DBNull.Value) { com.Date = (DateTime)reader["OrderDate"]; } lstCommandes.Add(com); } var ligne = new LigneCommande(); ligne.IdProduit = (int)reader["ProductID"]; ligne.PrixUnitaire = (decimal)reader["UnitPrice"]; ligne.Quantité = (Int16)reader["Quantity"]; ligne.Réduction = (float)reader["Discount"]; lstCommandes.Last().LstLignesCommandes.Add(ligne); } }