LinqToXML

http://www.cnblogs.com/luckdv/articles/1728088.html?login=1

http://de.cel.blog.163.com/blog/static/5145123620119983945167/

Data.xml:

<Students>
<Student Id ="1">
<Name>张三</Name>
<Address>张三家地址</Address>
</Student>
<Student Id ="2">
<Name>李四</Name>
<Address>李四家地址</Address>
</Student>
</Students>

 

XMLHelper:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using StudentManager.Models;

namespace StudentManager
{
    public class XmlHelper
    {
        private XElement Root;

        private List<Student> _StuList;
        public List<Student> StuList
        {
            get { return _StuList; }
            set { _StuList = value; }
        }
        string path = HttpRuntime.AppDomainAppPath.ToString() + "App_Data\\Data.xml";

        public XmlHelper()
        {
            Root = Load();
        }
        public XElement Load()
        {
            XElement root = XElement.Load(path);
            return root;
        }
        public List<Student> GetAllStudents()
        {
            List<Student> list = new List<Student>();
            var students = Root.Elements("Student");
            foreach (var stu in students)
            {
                Student s = new Student();
                s.Id = Convert.ToInt32(stu.Attribute("Id").Value);
                s.Name = stu.Element("Name").Value;
                s.Address = stu.Element("Address").Value;
                list.Add(s);
            }
            StuList = list;          

            return list;
        }
        public Student GetStudentById(int id)
        {
            Student stu = new Student();

            IEnumerable<XAttribute> es = Root.Elements("Student").Attributes("Id");

            //XElement el = es.FirstOrDefault(o => o.Value == id.ToString()).Parent;
            
            
            XElement e = Root.Elements("Student").Single(o => 
            {
                bool flag = o.Attribute("Id").Value == id.ToString();
                if (flag)
                {                    
                    stu.Id = Convert.ToInt32(o.Attribute("Id").Value);
                    stu.Name = o.Element("Name").Value;
                    stu.Address = o.Element("Address").Value;
                }
                return flag;
            });            

            return stu;
        }
        public void DeleteStudentById(int id)
        {
            XElement t = (from stu in Root.Elements("Student")
                    where stu.Attribute("Id").Value == id.ToString()
                    select stu).Single() as XElement;
            t.Remove();           
            Root.Save(path);
        }
        public void UpdateStudent(Student stu)
        {
            //XElement e = Root.Elements("Student").Single(o => o.Attribute("Id").Value == stu.Id.ToString());
            //e.Element("Name").SetValue(stu.Name);
            //e.Element("Address").SetValue(stu.Address);
            //e.ReplaceWith(new XElement(stu.Name,stu.Address));
            Root.Elements("Student").Single(o => o.Attribute("Id").Value == stu.Id.ToString())
                .ReplaceWith(new XElement("Student",new XAttribute("Id",stu.Id),
                    new XElement("Name",stu.Name),
                    new XElement("Address",stu.Address)));
            Root.Save(path);
        }
        public void CreateStudent(Student stu)
        {
            int a = GetMaxIndex();
            stu.Id = a;

             XElement e = Root.Elements("Student").Last();
             e.AddAfterSelf(new XElement("Student"
                 ,new XAttribute("Id",stu.Id)
                 ,new XElement("Name",stu.Name)
                 ,new XElement("Address",stu.Address)));
            Root.Save(path);
        }

        private int GetMaxIndex()
        {
            //int temp = 0;
            int maxIndex = Root.Elements("Student").Max(o => Convert.ToInt32(o.Attribute("Id").Value));
            //Root.Elements("Student").Max((o) =>
            //{
            //    temp = (int)o.Attribute("Id");
            //    return temp;
            //});

            return ++maxIndex;
        }
    }
}

 

posted @ 2014-05-13 00:43  pnljs  阅读(276)  评论(0编辑  收藏  举报