I posted this to the xml group but got no responses, so I thought I'd
try here.
I have the following small class which inherits from the generic List
class:
[Serializable, XmlRoot("FoodItemList")]
public class FoodItemList : BindingList<FoodItem>
{
private bool _hasChanges = false;
[XmlAttribute("HasChanges")]
public bool HasChanges
{
get { return _hasChanges; }
set { _hasChanges = value; }
}
private DateTime _lastUpdated = DateTime.MinValue;
[XmlAttribute("LastUpdated")]
public DateTime LastUpdated
{
get { return _lastUpdated; }
set { _lastUpdated = value; }
}
}
Basically, I wanted to add a couple of properties and have them be
serialized as attributes with the class. Here is the xml that is
produced:
<?xml version="1.0" encoding="utf-8"?>
<FoodItemList>
<FoodItem Description="This is the description" UnitPrice="1.85"
SoldByWeight="false" ItemType="Entree" ItemMeal="Both" Id="1234" />
</FoodItemList>
Here is the code I use to serialize them:
private void btnSave_Click(object sender, EventArgs e)
{
FoodItemList foods = new FoodItemList();
FoodItem fi = new FoodItem();
fi.Id = 1234;
fi.Description = "This is the description";
fi.ItemMeal = FoodItemMeal.Both;
fi.ItemType = FoodItemType.Entree;
fi.SoldByWeight = false;
fi.UnitPrice = 1.85m;
foods.Add(fi);
foods.HasChanges = false;
foods.LastUpdated = DateTime.Now;
StreamWriter sw = new StreamWriter(@"c:\testser.xml");
XmlSerializer xs = new XmlSerializer(typeof(FoodItemList));
XmlSerializerNamespaces xsn = new
XmlSerializerNamespaces();
xsn.Add("", "");
xs.Serialize(sw, foods, xsn);
sw.Close();
}
My two added properties are not serialized. Not as properties nor as
elements. How can I get them to serialize?
Thanks,
Chris