views:

81

answers:

1

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:

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

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

    [Required]
    [DisplayName("Description")]
    [XmlElement("employeeDescription")]
    public string employeeDescription { get; set; }

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

    [Required]
    [DisplayName("ID: ")]
    [XmlElement("employeeID")]
    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" ?>
<employees>
<employee>
    <employeeName>David parker</employeeName>
     <employeePosition>Senior Web Developer</employeePosition>
     <employeeDescription>This is a test description<br>feele free to add something here.</employeeDescription>
     <employeePhoto>mypic.jpg</employeePhoto>
    <employeeID>1</employeeID></employee></employees>

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 asp.net mvc 2 rc 2

thanks

+2  A: 

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.

Jacob
as in select new { Model.name = xml.Element("name").value }and if so i take it i just declase model as new EmployeeModel right ???
minus4
Added code sample (untested)
Jacob
thanks for your help its good but i get error: <employee xmlns=''> was not expected. on line select serializer.Deserialize(xml.CreateReader()) as EmployeesModel;i have updated my post to include the XML if this helps !!!! thinking do i have to maybe set some kind of loop first and then deserialize each item as new EmployeeModel ???? thanks
minus4
The `XmlSerializer` class assumes a direct mapping of element names to properties. You can decorate your `EmployeesModel` class with attributes to describe how to do the mapping (see http://msdn.microsoft.com/en-us/library/83y7df3e(VS.71).aspx). Or you could use projection in your LINQ instead of an `XmlSerializer` (I'll add a sample shortly).
Jacob
your right it works right away now, my mistake was the <br /> in my description. as it is an xml tag, so i just need to html encode my descriptions before entry and everything works like a charm.
minus4