using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Reflection; using System.Data.SqlClient; namespace XMLExtractor { public class Table { public int idCapitolo { get; set; } public int idSezione { get; set; } public int idOrdinamento { get; set; } public int idPagina { get; set; } public string posizione { get; set; } } public class TableEvoluzione: Table { public List columns { get; set; } public List rows { get; set; } public List note { get; set; } // NEW public string descrizione { get; set; } public List legenda { get; set; } public int writeColumns(SqlConnection conn) { string sqlCommand = "(idREP_IMM_MON_Column, column1, column2, column3, column4, column5) " + "VALUES (@idREP_IMM_MON_Column, @column1, @column2, @column3, @column4, @column5) "; int primarykey = Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_Columns", conn)).ExecuteScalar()) + 1; SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_Columns " + sqlCommand, conn); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Column", primarykey); for (int i = 1; i <= 5; i++ ) if (i <= this.columns.Count) insertTo.Parameters.AddWithValue("@column" + i,this.columns[i - 1]); else insertTo.Parameters.AddWithValue("@column" + i, DBNull.Value); int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); if (rowsUpdatedInserted != 0) return primarykey; return 0; } public void writeDB(SqlConnection conn) { string sqlCommand = "(idREP_IMM_MON_TableEvoluzione, idREP_IMM_MON_Pagina, idREP_IMM_MON_Column, descrizione, posizione, nota1, nota2, nota3, idREP_IMM_MON_Legenda) " + "VALUES (@idREP_IMM_MON_TableEvoluzione, @idREP_IMM_MON_Pagina, @idREP_IMM_MON_Column, @descrizione, @posizione, @nota1, @nota2, @nota3, @idREP_IMM_MON_Legenda) "; int primaryKey = Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_TableEvoluzione", conn)).ExecuteScalar()) + 1; int idColumn = writeColumns(conn); SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_TableEvoluzione " + sqlCommand, conn); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_TableEvoluzione", primaryKey); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Pagina", this.idPagina); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Column", idColumn); insertTo.Parameters.AddWithValue("@descrizione", this.descrizione); insertTo.Parameters.AddWithValue("@posizione", this.posizione); if(Legenda.writeDB(conn, this.legenda) != 0 ) insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Legenda", Legenda.writeDB(conn, this.legenda)); else insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Legenda", DBNull.Value); for (int i = 1; i <= 3; i++) { if (this.note != null && i <= this.note.Count) insertTo.Parameters.AddWithValue("@nota" + i, note[i-1].valore); else insertTo.Parameters.AddWithValue("@nota" + i, DBNull.Value); } int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); if (rowsUpdatedInserted != 0) foreach(var row in this.rows) row.writeDB(conn, idColumn); } public List getProperties() { List properties = new List(); TableEvoluzione objectChart = new TableEvoluzione(); foreach (var prop in objectChart.GetType().GetProperties()) { properties.Add(prop.Name.ToLower()); } properties.Remove("idcapitolo"); properties.Remove("idsezione"); properties.Remove("idordinamento"); return properties; } public static void writeTableEvoluzione(XmlNode attribute, List tagList, string nextTag, Data data) { List stringValueList = new List(); List rowsList = new List(); List noteList = new List(); TableEvoluzione tableEvoluzione = new TableEvoluzione(); foreach (XmlNode attr in attribute.ChildNodes) if (!tagList.Contains(attr.Name.ToString())) { PropertyInfo propertyInfo = tableEvoluzione.GetType().GetProperty(attr.Name); if (propertyInfo.PropertyType.Name.Equals("String")) propertyInfo.SetValue(tableEvoluzione, Convert.ChangeType(attr.InnerText, propertyInfo.PropertyType), null); else if(attr.Name.Equals("legenda")) propertyInfo.SetValue(tableEvoluzione, Convert.ChangeType(Legenda.getLegenda(attr), propertyInfo.PropertyType), null); else { foreach (XmlNode innerAttr in attr.ChildNodes) { switch (innerAttr.Name) { case "column": stringValueList.Add(innerAttr.InnerXml.ToString()); break; case "row": foreach (XmlNode column in innerAttr.ChildNodes) { TableEvoluzione.Rows rows = new TableEvoluzione.Rows(); stringValueList = new List(); foreach (XmlNode _row in column) stringValueList.Add(_row.InnerText); rows.row = stringValueList; rowsList.Add(rows); } break; case "nota": Nota nota = new Nota(); foreach (XmlNode row in innerAttr.ChildNodes) { PropertyInfo propertyInfoNota = nota.GetType().GetProperty(row.Name); propertyInfoNota.SetValue(nota, Convert.ChangeType(row.InnerXml.ToString(), propertyInfoNota.PropertyType), null); } noteList.Add(nota); break; } } switch (attr.Name) { case "columns": propertyInfo.SetValue(tableEvoluzione, Convert.ChangeType(stringValueList, propertyInfo.PropertyType), null); break; case "rows": propertyInfo.SetValue(tableEvoluzione, Convert.ChangeType(rowsList, propertyInfo.PropertyType), null); break; case "note": propertyInfo.SetValue(tableEvoluzione, Convert.ChangeType(noteList, propertyInfo.PropertyType), null); break; } stringValueList = new List(); } } tableEvoluzione.idCapitolo = ReadXML.idCapitolo; tableEvoluzione.idSezione = ReadXML.idSezione; tableEvoluzione.idOrdinamento = ReadXML.idOrdinamento; tableEvoluzione.idPagina = ReadXML.idPagina; ReadXML.idOrdinamento++; data.tableEvoluzioneList.Add(tableEvoluzione); } public class Rows { public List row { get; set; } public void writeDB(SqlConnection conn) { string sqlCommand = "(idREP_IMM_MON_Row, row1, row2, row3, row4, row5) VALUES " + "(@idREP_IMM_MON_Row, @row1, @row2, @row3, @row4, @row5)"; SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_Rows " + sqlCommand, conn); int primaryKey = Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_Rows", conn)).ExecuteScalar()) + 1; insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Row", primaryKey); for (int i = 1; i <= 5; i++) { if (this.row != null && (i-1) < this.row.Count) insertTo.Parameters.AddWithValue("@row" + i, row[i-1]); else insertTo.Parameters.AddWithValue("@row" + i, DBNull.Value); } int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); } public void writeDB(SqlConnection conn, int idColumn) { string sqlCommand = "(idREP_IMM_MON_Row, idREP_IMM_MON_Column, row1, row2, row3, row4, row5) VALUES " + "(@idREP_IMM_MON_Row, @idColumn, @row1, @row2, @row3, @row4, @row5)"; SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_Rows " + sqlCommand, conn); int primaryKey = Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_Rows", conn)).ExecuteScalar()) + 1; insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Row", primaryKey); insertTo.Parameters.AddWithValue("@idColumn", idColumn); for (int i = 1; i <= 5; i++) { if (this.row != null && (i - 1) < this.row.Count) insertTo.Parameters.AddWithValue("@row" + i, row[i - 1]); else insertTo.Parameters.AddWithValue("@row" + i, DBNull.Value); } int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); } } } public class TableImposta : Table { public List row { get; set; } public int writeColumns(SqlConnection conn, List columns) { string sqlCommand = "(idREP_IMM_MON_Column, column1, column2, column3, column4, column5) " + "VALUES (@idREP_IMM_MON_Column, @column1, @column2, @column3, @column4, @column5) "; int primarykey = Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_Columns", conn)).ExecuteScalar()) + 1; SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_Columns " + sqlCommand, conn); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Column", primarykey); for (int i = 1; i <= 5; i++) if (i <= columns.Count) insertTo.Parameters.AddWithValue("@column" + i, columns[i - 1]); else insertTo.Parameters.AddWithValue("@column" + i, DBNull.Value); int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); if (rowsUpdatedInserted != 0) return primarykey; return 0; } public void writeDB(SqlConnection conn) { List columns = new List(); columns.Add("Imposta"); columns.Add("Acconto (o versamento intera imposta)"); columns.Add("Saldo"); string sqlCommand = "(idREP_IMM_MON_TableImposta, idREP_IMM_MON_Pagina, idREP_IMM_MON_Column) " + "VALUES (@idREP_IMM_MON_TableImposta, @idREP_IMM_MON_Pagina, @idREP_IMM_MON_Column) "; int primarykey = Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_TableImposta", conn)).ExecuteScalar()) + 1; int idColumn = writeColumns(conn, columns); SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_TableImposta " + sqlCommand, conn); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_TableImposta", primarykey); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Pagina", this.idPagina); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Column", idColumn); int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); if (rowsUpdatedInserted != 0) foreach (var item in this.row) item.writeDB(conn, idColumn); } public List getProperties() { List properties = new List(); TableImposta objectChart = new TableImposta(); foreach (var prop in objectChart.GetType().GetProperties()) { properties.Add(prop.Name.ToLower()); } properties.Remove("idcapitolo"); properties.Remove("idsezione"); properties.Remove("idordinamento"); properties.Remove("idpagina"); return properties; } public class Rows { public List row { get; set; } public void writeDB(SqlConnection conn, int idColumn) { string sqlCommand = "(idREP_IMM_MON_Row, idREP_IMM_MON_Column, row1, row2, row3, row4, row5) VALUES " + "(@idREP_IMM_MON_Row, @idColumn, @row1, @row2, @row3, @row4, @row5)"; SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_Rows " + sqlCommand, conn); int primaryKey = Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_Rows", conn)).ExecuteScalar()) + 1; insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Row", primaryKey); insertTo.Parameters.AddWithValue("@idColumn", idColumn); for (int i = 1; i <= 5; i++) { if (this.row != null && (i - 1) < this.row.Count) insertTo.Parameters.AddWithValue("@row" + i, row[i - 1]); else insertTo.Parameters.AddWithValue("@row" + i, DBNull.Value); } int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); } } public static void writeTableImposta(XmlNode attribute, List tagList, string nextTag, Data data) { List stringValueList = new List(); List propertiesList = new List(); List rowsList = new List(); TableImposta tableImposta = new TableImposta(); propertiesList = tableImposta.getProperties(); foreach (XmlNode attr in attribute.ChildNodes) if (!tagList.Contains(attr.Name.ToString())) { PropertyInfo propertyInfo = tableImposta.GetType().GetProperty(attr.Name); if (propertyInfo.PropertyType.Name.Equals("String")) propertyInfo.SetValue(tableImposta, Convert.ChangeType(attr.InnerText, propertyInfo.PropertyType), null); else { TableImposta.Rows rows = new TableImposta.Rows(); stringValueList = new List(); foreach (XmlNode innerAttr in attr.ChildNodes) stringValueList.Add(innerAttr.InnerText); rows.row = stringValueList; rowsList.Add(rows); } if (attr.NextSibling == null) propertyInfo.SetValue(tableImposta, Convert.ChangeType(rowsList, propertyInfo.PropertyType), null); } tableImposta.idCapitolo = ReadXML.idCapitolo; tableImposta.idSezione = ReadXML.idSezione; tableImposta.idOrdinamento = ReadXML.idOrdinamento; tableImposta.idPagina = ReadXML.idPagina; ReadXML.idOrdinamento++; data.tableImpostaList.Add(tableImposta); } } public class TableCittaPrincipali : Table { public List row { get; set; } // NEW public string descrizione { get; set; } public List legenda { get; set; } public void writeCittaPrincipali(SqlConnection conn, int primarykey) { string sqlCommand = "(idREP_IMM_MON_CittaPrincipale, idREP_IMM_MON_TableCittaPrincipale, citta, trend, centroMedioAcquisto, " + "periferiaMedioAcquisto, centroMedioAffitto, periferiaMedioAffitto, centroRedditoMedio, periferiaRedditoMedio) " + "VALUES (@idREP_IMM_MON_CittaPrincipale, @idREP_IMM_MON_TableCittaPrincipale, @citta, @trend, @centroMedioAcquisto, " + "@periferiaMedioAcquisto, @centroMedioAffitto, @periferiaMedioAffitto, @centroRedditoMedio, @periferiaRedditoMedio)"; foreach (var item in this.row) { SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_CittaPrincipali " + sqlCommand, conn); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_TableCittaPrincipale", primarykey); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_CittaPrincipale", Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_CittaPrincipali", conn)).ExecuteScalar()) + 1); insertTo.Parameters.AddWithValue("@citta", item.citta); insertTo.Parameters.AddWithValue("@trend", item.trendVivacita); insertTo.Parameters.AddWithValue("@centroMedioAcquisto", item.prezzoMedioAcquisto.centro); insertTo.Parameters.AddWithValue("@periferiaMedioAcquisto", item.prezzoMedioAcquisto.periferia); insertTo.Parameters.AddWithValue("@centroMedioAffitto", item.prezzoMedioAffitto.centro); insertTo.Parameters.AddWithValue("@periferiaMedioAffitto", item.prezzoMedioAffitto.periferia); insertTo.Parameters.AddWithValue("@centroRedditoMedio", item.redditivitaMedia.centro); insertTo.Parameters.AddWithValue("@periferiaRedditoMedio", item.redditivitaMedia.periferia); int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); } } public void writeDB(SqlConnection conn) { string sqlCommand = "(idREP_IMM_MON_TableCittaPrincipale, idREP_IMM_MON_Pagina, descrizione, posizione, idREP_IMM_MON_Legenda) " + "VALUES (@idREP_IMM_MON_TableCittaPrincipale, @idREP_IMM_MON_Pagina, @descrizione, @posizione, @idREP_IMM_MON_Legenda) "; int primaryKey = Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_TableCittaPrincipali", conn)).ExecuteScalar()) + 1; SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_TableCittaPrincipali " + sqlCommand, conn); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Pagina", this.idPagina); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_TableCittaPrincipale", primaryKey); insertTo.Parameters.AddWithValue("@descrizione", this.descrizione); insertTo.Parameters.AddWithValue("@posizione", this.posizione); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Legenda", Legenda.writeDB(conn, this.legenda)); int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); if (rowsUpdatedInserted != 0) writeCittaPrincipali(conn, primaryKey); } public List getProperties() { List properties = new List(); TableCittaPrincipali objectChart = new TableCittaPrincipali(); foreach (var prop in objectChart.GetType().GetProperties()) { properties.Add(prop.Name.ToLower()); } properties.Remove("idcapitolo"); properties.Remove("idsezione"); properties.Remove("idordinamento"); properties.Remove("idpagina"); return properties; } public static void writeCittaPrincipali(XmlNode attribute, List tagList, string nextTag, Data data) { List legendaList = new List(); TableCittaPrincipali tableCittaPrincipali = new TableCittaPrincipali(); List tableCittaPrincipaliAttributes = tableCittaPrincipali.getProperties(); List rowList = new List(); foreach (XmlNode attr in attribute.ChildNodes) if (!tagList.Contains(attr.Name.ToString())) { PropertyInfo propertyInfo = tableCittaPrincipali.GetType().GetProperty(attr.Name); if (!attr.Name.Equals("row") && !attr.Name.ToLower().Equals("legenda")) { tableCittaPrincipaliAttributes.Remove(Utility.RemoveDiacritics(attr.Name.ToLower())); propertyInfo.SetValue(tableCittaPrincipali, Convert.ChangeType(attr.InnerText, propertyInfo.PropertyType), null); } else if (attr.Name.ToLower().Equals("legenda")) { legendaList = Legenda.getLegenda(attr); propertyInfo = tableCittaPrincipali.GetType().GetProperty(attr.Name); propertyInfo.SetValue(tableCittaPrincipali, Convert.ChangeType(legendaList, propertyInfo.PropertyType), null); } else rowList.Add(RowCittaPrincipali.getRowCittaPrincipali(attr)); tableCittaPrincipaliAttributes.Remove(Utility.RemoveDiacritics(attr.Name.ToLower())); if (tableCittaPrincipaliAttributes.Count > 0 && attr.NextSibling.Name.Equals("legenda")) { propertyInfo.SetValue(tableCittaPrincipali, Convert.ChangeType(rowList, propertyInfo.PropertyType), null); tableCittaPrincipaliAttributes.Remove(Utility.RemoveDiacritics(attr.Name.ToLower())); } if (!(tableCittaPrincipaliAttributes.Count > 0)) { tableCittaPrincipali.idCapitolo = ReadXML.idCapitolo; tableCittaPrincipali.idSezione = ReadXML.idSezione; tableCittaPrincipali.idOrdinamento = ReadXML.idOrdinamento; tableCittaPrincipali.idPagina = ReadXML.idPagina; ReadXML.idOrdinamento++; data.cityList.Add(tableCittaPrincipali); tableCittaPrincipali = new TableCittaPrincipali(); tableCittaPrincipaliAttributes = tableCittaPrincipali.getProperties(); } } else ReadXML.extractTag(attr, attr.Name.ToString()); } } public class RowCittaPrincipali { public string citta { get; set; } public string trendVivacita { get; set; } public PrezzoMedio prezzoMedioAcquisto { get; set; } public PrezzoMedio prezzoMedioAffitto { get; set; } public PrezzoMedio redditivitaMedia { get; set; } public List getProperties() { List properties = new List(); RowCittaPrincipali objectChart = new RowCittaPrincipali(); foreach (var prop in objectChart.GetType().GetProperties()) { properties.Add(prop.Name.ToLower()); } properties.Remove("idcapitolo"); properties.Remove("idsezione"); properties.Remove("idordinamento"); properties.Remove("idpagina"); return properties; } public static RowCittaPrincipali getRowCittaPrincipali(XmlNode attribute) { RowCittaPrincipali row = new RowCittaPrincipali(); foreach (XmlNode attr in attribute.ChildNodes) { PropertyInfo propertyInfo = row.GetType().GetProperty(Utility.RemoveDiacritics(attr.Name)); if (propertyInfo.PropertyType.Name.Equals("String")) propertyInfo.SetValue(row, Convert.ChangeType(translateStringInSymbol(attr.InnerText), propertyInfo.PropertyType), null); else propertyInfo.SetValue(row, Convert.ChangeType(PrezzoMedio.getPrezzoMedio(attr), propertyInfo.PropertyType), null); } return row; } public static string translateStringInSymbol(string trend) { string result = ""; switch (trend) { case "=": result = "horizontalArrow.png"; break; case "-": result = "downArrow.png"; break; case "+": result = "upArrow.png"; break; default: result = trend; break; } return result; } } public class PrezzoMedio { public string centro { get; set; } public string periferia { get; set; } public List getProperties() { List properties = new List(); PrezzoMedio objectChart = new PrezzoMedio(); foreach (var prop in objectChart.GetType().GetProperties()) { properties.Add(prop.Name.ToLower()); } properties.Remove("idcapitolo"); properties.Remove("idsezione"); properties.Remove("idordinamento"); properties.Remove("idpagina"); return properties; } public static PrezzoMedio getPrezzoMedio(XmlNode attribute) { PrezzoMedio prezzoMedio = new PrezzoMedio(); foreach (XmlNode attr in attribute.ChildNodes) { PropertyInfo propertyInfo = prezzoMedio.GetType().GetProperty(Utility.RemoveDiacritics(attr.Name.ToLower())); propertyInfo.SetValue(prezzoMedio, Convert.ChangeType(attr.InnerText, propertyInfo.PropertyType), null); } return prezzoMedio; } } }