• C#中DataGridView 对XML文档的使用


    窗体就只用添加一个DataGridView控件就可以了。详细解释请参照上一篇中的借鉴曲终人散博客园的文档。

    XML文档代码如下:test.xml

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <bookstore>
     3   <book type="必修课" ISBN="7-111-19149-2">
     4     <title>数据结构</title>
     5     <author>李</author>
     6     <price>30.2</price>
     7   </book>
     8   <book type="必修课" ISBN="7-111-19149-2">
     9     <title>数据结构</title>
    10     <author>李</author>
    11     <price>30.2</price>
    12   </book>
    13   <book type="必修课" ISBN="7-111-19149-2">
    14     <title>数据结构</title>
    15     <author>李</author>
    16     <price>30.2</price>
    17   </book>
    18   <book type="必修课" ISBN="7-111-19149-2">
    19     <title>数据结构</title>
    20     <author>李</author>
    21     <price>30.2</price>
    22   </book>
    23   <book type="必修课" ISBN="7-111-19149-2">
    24     <title>数据结构</title>
    25     <author>李</author>
    26     <price>30.2</price>
    27   </book>
    28   <book type="必修课" ISBN="7-111-19149-2">
    29     <title>数据结构</title>
    30     <author>李</author>
    31     <price>30.2</price>
    32   </book>
    33   <book type="必修课" ISBN="7-111-19149-2">
    34     <title>数据结构</title>
    35     <author>李</author>
    36     <price>30.2</price>
    37   </book>
    38   
    39 </bookstore>

    为了方便添加一个类:BookClass.cs

     1 namespace dataGridViewTest
     2 {
     3     public class BookClass
     4     {
     5         public BookClass()
     6         { }
     7         private string bookType;
     8 
     9         public string BookType
    10         {
    11             get { return bookType; }
    12             set { bookType = value; }
    13         }
    14         private string bookISBN;
    15 
    16         public string BookISBN
    17         {
    18             get { return bookISBN; }
    19             set { bookISBN = value; }
    20         }
    21         private string bookName;
    22 
    23         public string BookName
    24         {
    25             get { return bookName; }
    26             set { bookName = value; }
    27         }
    28         private string bookAuthor;
    29 
    30         public string BookAuthor
    31         {
    32             get { return bookAuthor; }
    33             set { bookAuthor = value; }
    34         }
    35         private double bookPrice;
    36 
    37         public double BookPrice
    38         {
    39             get { return bookPrice; }
    40             set { bookPrice = value; }
    41         }
    42     }
    43 }

    然后在窗体得主代码中加入:

     1  public partial class Form1 : Form
     2     {
     3         public Form1()
     4         {
     5             InitializeComponent();
     6         }
     7         List<BookClass> bookModeList=new List<BookClass>();
     8         private void Form1_Load(object sender, EventArgs e)
     9         {
    10             XmlDocument doc = new XmlDocument();
    11             doc.Load(@"....	est.xml");
    12 
    13             XmlNode xn = doc.SelectSingleNode("bookstore");
    14             XmlNodeList xnl = xn.ChildNodes;
    15             foreach (XmlNode xn1 in xnl)
    16             {
    17                 BookClass bookClass = new BookClass();
    18                 XmlElement xe = (XmlElement)xn1;
    19                 bookClass.BookISBN = xe.GetAttribute("ISBN").ToString();
    20                 bookClass.BookType = xe.GetAttribute("type").ToString();
    21 
    22                 XmlNodeList xn10 = xe.ChildNodes;
    23                 bookClass.BookName = xn10.Item(0).InnerText;
    24                 bookClass.BookAuthor = xn10.Item(1).InnerText;
    25                 bookClass.BookPrice = Convert.ToDouble(xn10.Item(2).InnerText);
    26                 bookModeList.Add(bookClass);
    27             }
    28             dataGridView1.DataSource=bookModeList;
    29         }
    30     }

    最终效果如下:

    目前还在学习中,希望会对大家有所帮助,觉得不错,就点赞支持一下。 另外,转载时请附带链接。谢谢!
  • 相关阅读:
    从简单需求到OLAP的RANK系列函数
    数据库的Index Scan V.S. Rscan
    z/OS上Dataset 的移动
    如何保存CONSOLE LOG
    c#对文件进行MD5加密校验
    基于webpivottable做的透视表
    通过asp.net程序来控制自己开发的windows服务
    AES加密和解密
    C#添加日志
    cmd执行mssql脚本或者执行mysql脚本
  • 原文地址:https://www.cnblogs.com/dangkai/p/7845103.html
Copyright © 2020-2023  润新知