zoukankan      html  css  js  c++  java
  • linq to xml(增删改查)

    xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <bookstore>
      <!--记录书本的信息-->
      <book Type="必修课" ISBN="7-111-19149-2">
        <title>数据结构</title>
        <author>严蔚敏</author>
        <price>30.00</price>
      </book>
      <book Type="必修课" ISBN="7-111-19149-3">
        <title>路由型与交换型互联网基础</title>
        <author>程庆梅</author>
        <price>27.00</price>
      </book>
      <book Type="选修课" ISBN="7-111-19149-1">
        <title>计算机操作系统</title>
        <author>张文明</author>
        <price>28</price>
      </book>
    </bookstore>

    加载本地xml

     string path = @"C:UsersasusDesktop	est.xml";
            XElement xe;
            public MainWindow()
            {
                InitializeComponent();
               xe= XElement.Load(path);
            }

    自定义一个类

     public class BookModel
            {
                public BookModel()
                { }
                /// <summary>
                /// 所对应的课程类型
                /// </summary>
                private string bookType;
    
                public string BookType
                {
                    get { return bookType; }
                    set { bookType = value; }
                }
    
                /// <summary>
                /// 书所对应的ISBN号
                /// </summary>
                private string bookISBN;
    
                public string BookISBN
                {
                    get { return bookISBN; }
                    set { bookISBN = value; }
                }
    
                /// <summary>
                /// 书名
                /// </summary>
                private string bookName;
    
                public string BookName
                {
                    get { return bookName; }
                    set { bookName = value; }
                }
    
                /// <summary>
                /// 作者
                /// </summary>
                private string bookAuthor;
    
                public string BookAuthor
                {
                    get { return bookAuthor; }
                    set { bookAuthor = value; }
                }
    
                /// <summary>
                /// 价格
                /// </summary>
                private double bookPrice;
    
                public double BookPrice
                {
                    get { return bookPrice; }
                    set { bookPrice = value; }
                }
            
        }

      private void Button_Click(object sender, RoutedEventArgs e)
            {
                
                XElement record = new XElement(
                   new XElement("book",
                   new XAttribute("Type", "选修课"),
                  new XAttribute("ISBN", "7-111-19149-1"),
                   new XElement("title", "计算机操作系统"),
                   new XElement("author", "张文明"),
                  new XElement("price", 28.00)));
                             xe.Add(record);
                             xe.Save(path);
            }

      private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                foreach (var item in xe.Elements("book").Where(x => x.Attribute("ISBN").Value== "7-111-19149-1").ToList())
                {
                    item.Remove();
                }
                xe.Save(path);
            }

     private void Button_Click_2(object sender, RoutedEventArgs e)
            {
                foreach (var item in xe.Elements("book").Where(x => x.Attribute("ISBN").Value == "7-111-19149-1").ToList())
                {
                    item.Element("title").Value = "test";
                }
                xe.Save(path);
            }
     

       private void Button_Click_3(object sender, RoutedEventArgs e)
            {
                List<BookModel> bookModelList = new List<BookModel>();
                foreach (var item in xe.Elements("book").Where(x => x.Attribute("ISBN").Value == "7-111-19149-1").ToList())
                {
                    
                    BookModel bookModel = new BookModel();
                    bookModel.BookType = item.Attribute("Type").Value;
                    bookModel.BookISBN = item.Attribute("ISBN").Value;
                    bookModel.BookName = item.Element("title").Value;
                    bookModel.BookPrice =Convert.ToDouble( item.Element("price").Value);
                    bookModel.BookAuthor = item.Element("author").Value;
                    bookModelList.Add(bookModel);
                }
                dg1.ItemsSource = bookModelList;
            }

     参考博客:https://www.cnblogs.com/yuer20180726/p/10984234.html

  • 相关阅读:
    Visual Studio 中创建带有向导的项目模板
    通过.NET Remoting调用CCNET接口的方法
    django单元测试历险记
    我的测试生活感悟3 淘宝的接口测试白皮书
    期待《 Beautiful Testing 》( 测试之美 )
    我的测试生活感悟4 谈谈面试
    NancyBlog 我的Google App Engine Blog
    我的测试生活感悟1
    我的测试生活感悟2 Art Of Unit Testing
    《微软的软件测试之道》(How We Test Software at Microsoft)
  • 原文地址:https://www.cnblogs.com/dangnianxiaoqingxin/p/13180853.html
Copyright © 2011-2022 走看看