C# Manipulating Object of type T



I'm using Xmlserializer to load/save xml, as objects. What I'm trying to do is to display the object from my load-method into a combobox. I'm loading the xml file "Categories.Xml" through my load method and it returns a object of a generic type (T).



public class XmlHandler<T> where T : class
{

internal XmlSerializer Xml;

public XmlHandler()
{

Xml = new XmlSerializer(typeof (T));
}

public void SaveXml(T datatyp, string path)
{
using (var streamWriter = new StreamWriter(path))
{
Xml.Serialize(streamWriter, datatyp);
}
}

public T Load(string path)
{
using (var streamReader = new StreamReader(path))
{
return Xml.Deserialize(streamReader) as T;
}


I'm trying to use this in my CategoryService Class that looks as Follow:



public class CategoryService
{
private Repository<ListOfCategories> repository = new Repository<ListOfCategories>();
private ListOfCategories createCategories = new ListOfCategories();
private string path = "Category.xml";

public List<Category> setCategories(string tbCategory)
{
List<Category> categoryList = new List<Category>();
var Category = new Category();

Category.Id = new Guid();
Category.CategoryName = tbCategory;

categoryList.Add(Category);
return categoryList;
}

public void saveCatgories(string tbCategory)
{
createCategories = repository.Load(path);
repository.Save(createCategories, path);

var newCategory = new Category()
{
Id = Guid.NewGuid(),
CategoryName = tbCategory
};
createCategories.AddCategory(newCategory);
repository.Save(createCategories, path);


}


The following code shows you how ListOfCategories looks like.



public class ListOfCategories : List
{
public new Guid Id { get; set; }

public List<Category> CategoryList { get; set; }


public ListOfCategories()
{
CategoryList = new List<Category>();
}

public void AddCategory(Category category)
{
CategoryList.Add(category);
}
}




  • I would really appreciate your help. I need to take the Loaded data (from the xmlfile using my load method), display it in a combobox (IE. there is a category class with the property Name, how do i select that Name from the Object and display it in a combobox)...




  • My Conclusion is - add the object to a list, iterate through the list, select what I want in some way and display it / do whatever I want with it...




  • Thank you for your time if you read this. This is really importand to me.




No comments:

Post a Comment