• 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;
            }
        }
    }
  • 相关阅读:
    C#根据用户输入字符串,输出大写字母有几个,小写字母有几个
    C#把大写英文变成小写英文,把小写英文变成大写英文
    C#中去除字符串里的多个空格且保留一个空格
    工作中遇到的99%SQL优化,这里都能给你解决方案(二)
    MySQL如何选择合适的索引
    工作中遇到的99%SQL优化,这里都能给你解决方案
    周期性线程池与主要源码解析
    Mysql关键字Explain 性能优化神器
    Mysql Explain详解
    Executor线程池只看这一篇就够了
  • 原文地址:https://www.cnblogs.com/pnljs/p/3724602.html
Copyright © 2020-2023  润新知