在C#中,可以使用XML數據綁定到界面控件,具體步驟如下:
<?xml version="1.0"?>
<Books>
<Book>
<Title>Book 1</Title>
<Author>Author 1</Author>
</Book>
<Book>
<Title>Book 2</Title>
<Author>Author 2</Author>
</Book>
</Books>
using System;
using System.Windows.Forms;
using System.Xml;
namespace XMLBindingExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
LoadData();
}
private void LoadData()
{
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
XmlNodeList nodes = doc.SelectNodes("//Book");
foreach (XmlNode node in nodes)
{
ListViewItem item = new ListViewItem(node.SelectSingleNode("Title").InnerText);
item.SubItems.Add(node.SelectSingleNode("Author").InnerText);
listView1.Items.Add(item);
}
}
}
}
listView1.View = View.Details;
listView1.Columns.Add("Title", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Author", 100, HorizontalAlignment.Left);
通過以上步驟,就可以實現將XML數據綁定到界面上的ListView控件中顯示。