Linq学习__04__数据源是XML

前端之家收集整理的这篇文章主要介绍了Linq学习__04__数据源是XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

想运行本程序,请先做好准备工作,请参考:Linq学习__00__准备工作

(1)在HomeController中新建Index2方法代码1.1


代码1.1

#region 数据源是XML
        /// <summary>
        /// 数据源是XML
        /// 访问地址 Home/Index2
        /// XML
        /// </summary>
        /// <returns></returns>
        public ActionResult Index2()
        {
            // 创建学生类
            List<Student> students = new List<Student>()
            {
                new Student {First="二代",Last="富",ID=111,Street="123 Main Street",City="大连",scores= new List<int> {97,92,81,60}},new Student {First="二麻子",Last="王",ID=112,Street="124 Main Street",City="深圳",scores= new List<int> {75,84,91,39}},new Student {First="吊丝",Last="项",ID=113,Street="125 Main Street",City="上海",scores= new List<int> {88,94,65,91}},};
            var studentsToXML = new XElement("Root",from student in students
                                                     let x = String.Format("{0},{1},{2},{3}",student.scores[0],student.scores[1],student.scores[2],student.scores[3])
                                                     select new XElement("student",new XElement("First",student.First),new XElement("Last",student.Last),new XElement("scores",x)
                                              ) // end "student"
                                ); // end "Root"
            ViewBag.studentsToXML = studentsToXML;
            return View();//返回View视图
        }
        #endregion 数据源是XML

2)添加视图Index2.cshtml,代码代码1.2


代码1.2

@{
    ViewBag.Title = "Index2";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{
    <div style=" background-color: Yellow;">
        数据源是XML</div> 
    @ViewBag.studentsToXML
}
原文链接:https://www.f2er.com/xml/299327.html

猜你在找的XML相关文章