ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

linq to xml(增删改查)

2020-06-23 10:51:33  阅读:240  来源: 互联网

标签:xml string 改查 Value xe item private linq new


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>

加载

 string path = @"C:\Users\asus\Desktop\test.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;
        }

 

标签:xml,string,改查,Value,xe,item,private,linq,new
来源: https://www.cnblogs.com/dangnianxiaoqingxin/p/13180853.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有