stuck binding xml to Model Class

[Originally Posted By]:

I am experimenting with using xml as a database for small CMS, like gallery or staff profiles etc

however being all subsonic minded i am stuck on how i bind my xml document to a modelclass so that i can then use that class for strongly typed views:

here is my model class:

public class EmployeesModel
    [DisplayName("Name: ")]
    public string employeeName { get; set; }

    [DisplayName("Position: ")]
    public string employeePosition { get; set; }

    public string employeeDescription { get; set; }

    [DisplayName("Photo: ")]
    public string employeePhoto { get; set; }

    [DisplayName("ID: ")]
    public int employeeID { get; set; }

and here is my code:

XDocument xmlDoc = XDocument.Load(Server.MapPath("~/App_Data/employees.xml"));

var model = (from xml in xmlDoc.Descendants("employee")
                  select xml) as IEnumerable<EmployeesModel>;

return View(model);

the XML

<?xml version="1.0" encoding="utf-8" ?>
    <employeeName>David parker</employeeName>
    <employeePosition>Senior Web Developer</employeePosition>
    <employeeDescription>This is a test description<br>feel free to add something here.</employeeDescription>

the xml side work but model is always empty, however i get no runtime erros when trying to bind, i know there is more i should do here but i need some help.

for clarity i am using mvc 2 rc 2



You need to deserialize the XML into objects. You cannot simply cast XML as objects. When you say as IEnumerable<EmployeesModel>, you’ll get a null since the types aren’t compatible. Your code could look something like this:

var serializer = new XmlSerializer(typeof(EmployeesModel));
var model = 
    from xml in xmlDoc.Descendants("employee")
    select serializer.Deserialize(xml.CreateReader()) as EmployeesModel;

Another option you could consider is to project the XElements into EmployeesModel objects, like this:

var model =
    from xml in xmlDoc.Descendants("employee")
    select new EmployeesModel {
        employeeName = (string)xml.Element("employeeName"),
        employeePosition = (string)xml.Element("employeePosition"),
        employeeDescription = (string)xml.Element("employeeDescription"),
        employeePhoto = (string)xml.Element("employeePhoto"),
        employeeID = (int)xml.Element("employeeID"), };

As you can see, that can get tedious. However, it may be appropriate. If your XML file represents all employee data but your view shows only a subset of the data or the data in a different structure, you probably don’t want your view model to be a direct copy of the contents of your data store.


2 thoughts on “stuck binding xml to Model Class

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s