我正在将现有的WebForms应用程序转换为MVC5。有数据存储在 / App_Data 文件夹中的XML文件中。我创建了一个包含所有匹配项的模型。我需要从此XML文件中获取数据,然后将其作为 IQueryable 返回。
最有效/最佳实践方法是什么?任何简单的例子都会很棒。
澄清 我知道从XML获取数据的各种方法。问题更多地是关于将数据导入IQueryable。
这是我的模特:
:
这是我的XML文档:
using System;
using System.ComponentModel.DataAnnotations;
namespace Framework.Models
{
public class NewsModel
{
public NewsModel()
{
Title = string.Empty;
Summary = string.Empty;
Image = string.Empty;
ImageWidth = 0;
ImageHeight = 0;
ImageSrcSet = string.Empty;
ImageSizes = string.Empty;
Url = new Uri("#");
UrlText = string.Empty;
UrlTarget = "_self";
Date = DateTime.Now;
}
[Display(Name = "Title")]
public String Title { get; set; }
[Display(Name = "Summary")]
public String Summary { get; set; }
[Display(Name = "Image")]
public String Image { get; set; }
[Display(Name = "ImageWidth")]
public int ImageWidth { get; set; }
[Display(Name = "ImageHeight")]
public int ImageHeight { get; set; }
[Display(Name = "ImageSrcSet")]
public String ImageSrcSet { get; set; }
[Display(Name = "ImageSizes")]
public String ImageSizes { get; set; }
[Display(Name = "Url")]
public Uri Url { get; set; }
[Display(Name = "UrlText")]
public String UrlText { get; set; }
[Display(Name = "UrlTarget")]
public String UrlTarget { get; set; }
[Display(Name = "Date")]
public DateTime Date { get; set; }
}
}
答案 0 :(得分:2)
这将帮助您入门
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication43
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlSerializer xs = new XmlSerializer(typeof(Framework.Models.News));
XmlTextReader reader = new XmlTextReader(FILENAME);
Framework.Models.News news = (Framework.Models.News)xs.Deserialize(reader);
}
}
}
namespace Framework.Models
{
[XmlRoot("News")]
public class News
{
[XmlElement("Item")]
public List<NewsModel> newsModel { get; set; }
}
[XmlRoot("Item")]
public class NewsModel
{
public NewsModel()
{
Title = string.Empty;
Summary = string.Empty;
Image = string.Empty;
ImageWidth = 0;
ImageHeight = 0;
ImageSrcSet = string.Empty;
ImageSizes = string.Empty;
Url = "#";
UrlText = string.Empty;
UrlTarget = "_self";
//Date = DateTime.Now;
}
[XmlElement("Title")]
public String Title { get; set; }
[XmlElement("Summary")]
public String Summary { get; set; }
[XmlElement("Image")]
public String Image { get; set; }
[XmlElement("ImageWidth")]
public int ImageWidth { get; set; }
[XmlElement("ImageHeight")]
public int ImageHeight { get; set; }
[XmlElement("ImageSrcSet")]
public String ImageSrcSet { get; set; }
[XmlElement("ImageSizes")]
public String ImageSizes { get; set; }
[XmlElement("Url")]
public string Url { get; set; }
[XmlElement("UrlText")]
public String UrlText { get; set; }
[XmlElement("UrlTarget")]
public String UrlTarget { get; set; }
[XmlElement("Date")]
public string Date { get; set; }
}
}
答案 1 :(得分:1)
如果你有代码来创建你的对象,你可以把它变成IQueryable,如下所示:
NewsModel newsModel = CreateNewsModel();
var query = new [] { newsModel }.AsQueryable();