Tengo un fichero en XML para poder llenar varios datos de un drop down, pero no se si no se deseariliza de manera correcta o estoy mal en mi código ya que cuando lo ejecuto, no muestra ningún valor dentro del DropDown
Aqui es donde creo la lista y el nombre del fichero
public List<Data.Users> UserList()
{
string myConnectionString = @"C:\Users\gutiece\Desktop\" + "tool_track.accdb";
List<Data.Users> UserList = new List<Data.Users>();
OleDbConnection connection = new OleDbConnection();
OleDbCommand command = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
try
{
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
bool ok = System.IO.File.Exists(myConnectionString);
String qry = "SELECT * FROM users AS Users";
command.Connection = connection;
command.CommandText = qry;
adapter.SelectCommand = command;
command.Connection.Open();
OleDbDataReader reader = command.ExecuteReader(); // close conn after
while (reader.Read())
{
UserList.Add(new Data.Users(
reader["WHO"].ToString(),
Convert.ToInt32(reader["PAYROLL_NUMBER"].ToString()),
reader["NAME"].ToString(),
reader["OU"].ToString(),
Convert.ToInt32(reader["POOL"].ToString()),
Convert.ToInt32(reader["TEAM"].ToString()),
reader["PASS"].ToString(),
reader["ROL"].ToString()));
}
//Save Lists in temp folder
XML.ToXML XMLs = new XML.ToXML();
XMLs.SerializeXMLUsers(UserList, "MyListOfUsers");
List<Data.Users> AllShort = XMLs.BuilDeSerializingUsers("MyListOfUsers");
String Model = AllShort[0].NAME;
if (!reader.IsClosed)
{
reader.Close();
}
return UserList;
}
catch (Exception)
{
}
return UserList;
}
Esta es la serializacion y deserealizacion del XML
public void SerializeXMLUsers<T>(List<T> UserList, String FileName)
{
String imgPath = HttpContext.Current.Request.PhysicalApplicationPath + @"\FilesXML\" + FileName + ".xml";
String ImageFolderPath = HttpContext.Current.Request.PhysicalApplicationPath + @"\FilesXML\";
try
{
Type[] elements = { typeof(List<T>) };
XmlSerializer serializer = new XmlSerializer(typeof(List<T>), elements);
FileStream fs = new FileStream(imgPath, FileMode.Create);
serializer.Serialize(fs, UserList);
fs.Close();
UserList = null;
}
catch (Exception)
{
}
}
public List<Data.Users> BuilDeSerializingUsers(String FileName)
{
String imgPath = HttpContext.Current.Request.PhysicalApplicationPath + @"\FilesXML\" + FileName + ".xml";
String ImageFolder = HttpContext.Current.Request.PhysicalApplicationPath + @"\FilesXML";
List<Data.Users> UserList = null;
XmlSerializer serializer = new XmlSerializer(typeof(List<Data.Users>));
StreamReader reader = new StreamReader(imgPath);
UserList = (List<Data.Users>)serializer.Deserialize(reader);
reader.Close();
return UserList;
}
y esta es la parte donde quiero llenar el DropDown
:
private void BindDpoTlXml()
{
String filePath = Server.MapPath("/FilesXML/MyListOfUsers.xml");
using (DataSet ds = new DataSet())
if (ds.Tables.Count > 0)
{
{
ds.ReadXml(filePath);
Dpo_tl.DataSource = ds;
Dpo_tl.DataTextField = "NAME";
Dpo_tl.DataValueField = "Select a Team Leader";
Dpo_tl.DataBind();
}
}
}
Los ficheros se crean correctamente en la carpeta asgnada pero por alguna razon los ficheros no se leen correctamente.
Espero puedan ayudarme