linq to xml(增刪改查)

xml文件html

<?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:\Users\asus\Desktop\test.xml";
        XElement xe;
        public MainWindow()
        {
            InitializeComponent();
           xe= XElement.Load(path);
        }

自定義一個類spa

 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);
        }

code

  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);
        }

xml

 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);
        }

 

 

htm

   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.htmlblog

相關文章
相關標籤/搜索