坚持绑定到XML模型类 [英] stuck binding xml to Model Class
问题描述
我使用XML作为小型CMS数据库,如画廊或工作人员档案进行实验
等等
I am experimenting with using xml as a database for small CMS, like gallery or staff profiles etc
不过是所有亚音速志同道合我坚持我如何我的XML文档绑定到modelclass
这样我就可以使用这个类的强类型的意见:
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:
这是我的模型类:
[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; }
}
和这里是我的code:
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);
在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>
在XML一边工作,但模式永远是空的,但是我试图绑定时拿不出误差修改运行时,我知道还有更多我应该做的,但在这里我需要一些帮助。
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.
为了清楚起见,我使用asp.net MVC 2 RC 2
for clarity i am using asp.net mvc 2 rc 2
感谢
推荐答案
您需要的反序列化的XML转换的对象。你不能简单地施展XML作为对象。当你说为IEnumerable&LT; EmployeesModel&GT;
,你会得到一个空
,因为类型不兼容。您code可能是这个样子:
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;
您可以考虑另一种选择是的项目的的的XElement
s转换EmployeesModel对象,像这样的:
Another option you could consider is to project the XElement
s 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"), };
正如你所看到的,这可能会很麻烦。但是,它可能是适当的。如果您的XML文件重新presents所有员工的数据,但你的观点仅显示数据或在不同的结构中的数据的一个子集,你可能不希望您的视图模型是你数据的内容直接复制商店。
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.
这篇关于坚持绑定到XML模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!