Answered by:
Serialize /Deserialize XmlAttribute with Enum values and some default

Question
-
Hi,
MY XML:<ColProperty DBColumn="System" DisplayInGrid=" Mode" IsDisplay="true" IsSelecte="true" SelectedPosition="0" Coordinate="-1" IsClassifyFiled="false" />
The attribute Coordinate contain values (-1 till 4) when 0 till 4 is represented by Enum :
public enum COORD_SYSTEM { [XmlEnumAttribute("0")] COORD_SYSTEM_GEO = 0, // Display : GEO [XmlEnumAttribute("1")] COORD_SYSTEM_UTM = 1, // Display : UTM [XmlEnumAttribute("2")] COORD_SYSTEM_RSO = 2, // Display : RSO [XmlEnumAttribute("3")] COORD_SYSTEM_GEO_LEGACY = 3, // Display : GEO Legacy [XmlEnumAttribute("4")] COORD_SYSTEM_MRG = 4 // Display : MGR }
My XML is serialize and connect to DataGridView.
I want that when there is (-1) value in the XML attribute “Coordinate”, My cell in the DataGridView will be empty.
Is it possible?
I can't change the Enum...
- Moved by CoolDadTx Wednesday, May 4, 2016 1:46 PM Winforms related
Wednesday, May 4, 2016 12:48 PM
Answers
-
Seeing as you're working with attributes I suggest you use linq to xml to read and write to the xml file.
Define a class with all your attributes on it.
public class row() { public string DBColumn {get;set;} // etc
You can then new up a bindinglist<row> and bind your datagridview to that.
Your reading and writing are decoupled from the collection of objects so you can see what's going on and you have precise control over the row object so it's strongly typed.
When you want to serialise, you iterate the list and foreach you add an xelement colProperty. For each property, add an xattribute to that xelement.
To deserialise, do the reverse.
This may seem a bit sort of manual but you can put logic in for any of those attribute/property conversions should that prove necessary.
- Proposed as answer by Moonlight ShengMicrosoft contingent staff Tuesday, May 17, 2016 5:00 AM
- Marked as answer by Moonlight ShengMicrosoft contingent staff Wednesday, May 18, 2016 6:28 AM
Thursday, May 5, 2016 3:10 PM
All replies
-
I suggest you use a datagridviewcomboboxcolumn and see whether that suits you.
As outlined here:
http://stackoverflow.com/questions/56443/create-drop-down-list-options-from-enum-in-a-datagridview
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn(); col.Name = "My Enum Column"; col.DataSource = Enum.GetValues(typeof(MyEnum)); col.ValueType = typeof(MyEnum); dataGridView1.Columns.Add(col);
- Proposed as answer by Moonlight ShengMicrosoft contingent staff Tuesday, May 17, 2016 5:00 AM
Wednesday, May 4, 2016 2:44 PM -
Hi,
That will be great ,
But I need to Serialize/Deserialize the value too…
Thursday, May 5, 2016 6:33 AM -
Seeing as you're working with attributes I suggest you use linq to xml to read and write to the xml file.
Define a class with all your attributes on it.
public class row() { public string DBColumn {get;set;} // etc
You can then new up a bindinglist<row> and bind your datagridview to that.
Your reading and writing are decoupled from the collection of objects so you can see what's going on and you have precise control over the row object so it's strongly typed.
When you want to serialise, you iterate the list and foreach you add an xelement colProperty. For each property, add an xattribute to that xelement.
To deserialise, do the reverse.
This may seem a bit sort of manual but you can put logic in for any of those attribute/property conversions should that prove necessary.
- Proposed as answer by Moonlight ShengMicrosoft contingent staff Tuesday, May 17, 2016 5:00 AM
- Marked as answer by Moonlight ShengMicrosoft contingent staff Wednesday, May 18, 2016 6:28 AM
Thursday, May 5, 2016 3:10 PM -
Thanks Andy,
I'll try it.
Saturday, May 7, 2016 5:59 AM