Sunday, 19 April 2015

I have deserialized my XML file into class.cs, how can I use it in c# ViewModel



I have the following XML file that I would like to use in my ViewModel (the XML file has been simplified):



<?xml version="1.0" encoding="utf-8"?>
<FloorPlan xmlns="http://www.ioc.org">
<version>1.0</version>
<room type="bathroom">
<width>10</width>
<length>10</length>
<floor>tile</floor>
</room>
<room type="bedroom">
<width>15</width>
<length>20</length>
<furniture>
<bed>
<location></location>
</bed>
<dresser>
<location></location>
</dresser>
</furniture>
</room>
</FloorPlan>


I have used Visual Studio's xsd.exe tool to auto-generate a class floorplan.cs


I have used XmlSerializer.Deserialize to get the XML file into an instance of the class, myFloorplan


I have a User Interface that must display a complex visual representation of myFloorpan, allow the user to update it, and check business logic to ensure the user only makes valid updates. Once the user is finished, it gets written out to a newFloorplan.xml


How can I bind data in my UI to myFloorplan?


Adding INotifyPropertyChange in generated EF classes says not to implement INotifyPropertyChanged on the model, and instead use events. But either of these would require modifying the auto-generated code in floorplan.cs, which seems like a bad idea.


Another site suggests that auto-generated code isn't "WPF friendly", and as soon as I read floorplan.xml into myFloorplan that I should convert it to something that is data bindable within my view model, only converting it back if I need to write back to XML. But this seems like I would have two separate models that are almost identical, one that is auto-generated and one that I have a little more control over.


Any advice and quick code snippets would be greatly appreciated!


No comments:

Post a Comment