using System;using System.Windows.Forms;using System.Xml;namespace winformDemo{ public partial class Form2 : Form { public Form2() { InitializeCompone ...
using System;
using System.Windows.Forms;
using System.Xml;
namespace winformDemo
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//XmlDocument打開文檔
XmlDocument xc = new XmlDocument();
/// <summary>
/// 顯示所有商品的名字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
// xc.Load(寫入要獲取文檔的路徑)
xc.Load("D:\\Text\\shopping.xml");
//XmlNodeList xnl = xc.SelectSingleNode("shopping").ChildNodes;獲取shopping下麵的子節點放到集合里。
XmlNodeList xnl = xc.SelectSingleNode("shopping").ChildNodes;
//迴圈去取子節點
foreach (XmlNode xn in xnl)
{
//獲取商品名字。
//this.textBox1.Text += xn.Attributes["id"].Value + "\r\n";
foreach (XmlNode xnd in xn.ChildNodes)
{
if (xnd.Name == "Shopname")
{
textBox1.Text += "商品名字:" + xnd.InnerText + "\r\n";
}
}
}
}
/// <summary>
/// 顯示所有商品信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
this.textBox2.Text = "";
// xc.Load(寫入要獲取文檔的路徑)
xc.Load("D:\\Text\\shopping.xml");
//XmlNodeList xnl = xc.SelectSingleNode("shopping").ChildNodes;獲取shopping下麵的子節點放到集合里。
XmlNodeList xnl = xc.SelectSingleNode("shopping").ChildNodes;
//XmlNodeList xnl = xc.SelectSingleNode("/shopping/shop[last()]").ChildNodes;
//XmlNodeList xnl = xc.SelectSingleNode("/shopping/shop[@id=2]").ChildNodes;
//迴圈去取子節點
foreach (XmlNode xn in xnl)
{
//取最後一條數據
//textBox2.Text += "商品名字:" + xn.InnerText + "\r\n";
//獲取所有商品的信息。
textBox2.Text += "商品ID:" + xn.Attributes["id"].Value + "\r\n";
textBox2.Text += "商品名字:" + xn.ChildNodes[0].InnerText + "\r\n";
textBox2.Text += "商品價格:" + xn.ChildNodes[1].InnerText + "\r\n";
textBox2.Text += "商品數量:" + xn.ChildNodes[2].InnerText + "\r\n";
}
}
/// <summary>
/// 讀取所有信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
this.textBox3.Text = "";
XmlReader reader = XmlReader.Create(@"D:\\Text\\shopping.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Attribute:
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
this.textBox3.Text += "屬性:" + reader.Name + "=" + reader.Value + "\r\n";
}
break;
case XmlNodeType.Element:
this.textBox3.Text += "節點名稱:" + reader.LocalName + "\r\n";
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
this.textBox3.Text += "屬性:" + reader.Name + "=" + reader.Value + "\r\n";
}
break;
case XmlNodeType.Text:
this.textBox3.Text += "節點內容:" + reader.Value + "\r\n";
break;
}
}
}
/// <summary>
/// 添加信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
XmlWriter writer = XmlWriter.Create(@"D:\\Text\\goods.xml");
writer.WriteStartElement("dog");
writer.WriteAttributeString("color", "black");
writer.WriteString("How can I be sure");
writer.WriteEndDocument();
writer.Close();
}
}
}
添加XML 是直接添加進去了,沒有顯示出來。所以不要重覆點擊,可以去文本里看。
總結的一些:
XML
xml:
(超級標記語言)又稱(可擴展標記語言)(簡單的數據存儲語言)(數據存儲技術)
格式:
gb2312,UTF-8;
資料庫的存儲是XML格式。
特點:
跨平臺、跨語言、跨系統、跨瀏覽器、節點名自定義、封閉、區分大小寫、屬性自定義、唯一根節點。
C#提供XML的對象和方法:
XMLDocument(xml文檔):(load(路徑)、SelectSingleNode(獲取根節點)、childNodes(子節點))
XMLNodelist(節點集合):(XmlNodeList list=根節點下節點)
XMLNode(節點):(屬性:text、innerText、name、attributrs[]、value、childNodes)