解析XML【C#】
1.XML元素
XML元素包含一個開標記、元素中的數據、閉標記
例如:<book>book name</book>
其中book是元素名稱 book name是元素數據
元素名稱區分大小寫
每一個XML文檔中必須有一個根元素
2.XML屬性
屬性添加在元素的開標記內
<book titile = "book name"></book>或者<book title = 'book name'></book>
屬性的值可以用雙引號也可以用單引號
3.元素與屬性舉例
<book><title>book name</title></book>
<book title = "book name"></book>
上述兩種沒有本質的區別,但使用時建議使用第一種,因為可以對元素增加屬性或子元素,但是不能這樣操作屬性。
4.XML讀取解析
DOM解析XML文檔:XML的文檔結構是個樹狀結構
DOM的類在System.Xml中
XmlDocument:用于加載XML文件存放的磁盤位置
XmlNode:這個類表示文檔書中的一個節點
XmlNodeList:表示一個節點的集合
XmlElement:表示XML文檔中的一個元素
XmlAttribute:表示XML的一個屬性
XmlText:表示開標記和閉標記之間的文本
(1)XmlDocument:加載xml文檔
例:XmlDocument document = new XmlDocument();
document.load(@"D:\C#\books.xml");
(2)XmlElement:用于返回一個元素實例
XmlElement element = document.DocumentElement; //常用來返回一個根節點
XmlElement類包含許多方法和屬性,可以處理樹的節點和屬性
1)FirstChild:返回根元素節點后的第一個子節點
2)LastChild:返回根元素節點后的最后一個子節點
3)ParentNode:返回當前元素的父節點
4)NextSibling:返回當前節點的父節點下的下一個節點
5)HasChildNodes:用來檢查當前元素是否有子元素
(3)XmlText:表示開關標記之間的文本,是特殊的節點,屬性有:
1)InnerText:獲取當前節點范圍內的所有子節點的文本連接起來的字符串
2)InnerXml:獲取當前節點范圍內的所有子節點的文本連接起來的字符串,包含標記
5.XML修改解析
(1)創建節點
創建節點方法:
1)CreateElement:用來創建XmlDocument類型的節點
2)CreateAttribute:用來創建XmlAttribute類型的節點
3)CreateTextNode:用來創建XmlTextNode類型的節點
4)CreateNode:用來創建任意類型的節點,包含上述三種以及其他
創建節點后添加操作:
1)AppendChild:把創建的節點類型追加到XmlNode類型或其他派生類型的節點后
2)InsertAfter:將新節點插入到特定的位置
3)InsertBefore:將新節點插入到特定位置
(2)刪除節點
1)RemoveChild:刪除當前節點范圍內的一個指定的子節點
2)RemoveAll:刪除該節點范圍內的所有子節點
(3)修改節點
1)ReplaceChild:用新子節點替換舊子節點
6.XML查詢解析
(1)使用XmlDocument 方法
XmlDocument document = new XmlDocument();
document.Load(filePath);
XmlNodeList list = document.GetElementsByTagName("book");
foreach (XmlNode node in list)
{
listBox1.Items.Add(node.Name);//listBox1類型是ListBox
}
(2)使用XPath選擇特定的節點的方法
1)SelectSingleNode:用來選擇一個節點,如果有多個,則返回第一個節點
2)SelectNodes:返回一個節點的集合,類型是XmlNodesList
選擇特定的節點的方法參數XPath
XPath是XML文檔的查詢語言
7.XML解析代碼
- using System;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Xml;
- namespace Update
- {
- public partial class Form1 : Form
- {
- string filePath = Directory.GetCurrentDirectory() + @"\uploadXML.xml";
- public Form1()
- {
- InitializeComponent();
- }
- //讀取xml
- private void button1_Click(object sender, EventArgs e)
- {
- if (File.Exists(filePath))
- {
- XmlDocument document = new XmlDocument();
- document.Load(filePath);
- XmlNode root = (XmlNode)document.DocumentElement;
- //循環展示成樹狀
- xmlProcess(root,0);
- }
- }
- /*
- * 循環展示成樹狀
- */
- private void xmlProcess(XmlNode root,int i)
- {
- if (root == null)
- {
- return;
- }
- if (root is XmlElement)
- {
- listBox1.Items.Add(root.Name.PadLeft(root.Name.Length + i));
- if (root.HasChildNodes)
- {
- xmlProcess(root.FirstChild,i+2);
- }
- if (root.NextSibling != null)
- {
- xmlProcess(root.NextSibling, i);
- }
- }
- else if (root is XmlText)
- {
- string text = ((XmlText)root).Value;
- listBox1.Items.Add(text.PadLeft(text.Length+i));
- }
- }
- //讀取文本節點
- private void button2_Click(object sender, EventArgs e)
- {
- XmlDocument document = new XmlDocument();
- document.Load(filePath);
- XmlNode root = (XmlNode)document.DocumentElement;
- string values = root.InnerText;
- string values2 = root.InnerXml;
- string values3 = root.Value;
- MessageBox.Show(values);
- MessageBox.Show(values2);
- MessageBox.Show(values3);
- }
- //添加節點
- private void button3_Click(object sender, EventArgs e)
- {
- XmlDocument document = new XmlDocument();
- document.Load(filePath);
- XmlElement root = document.DocumentElement;
- //create node
- XmlElement newBook = document.CreateElement("book");
- XmlElement newTitle = document.CreateElement("title");
- XmlElement newAuthor = document.CreateElement("author");
- //create text
- XmlText titleText = document.CreateTextNode("new title");
- XmlText authorText = document.CreateTextNode("new author");
- //insert
- newTitle.AppendChild(titleText);
- newAuthor.AppendChild(authorText);
- newBook.AppendChild(newTitle);
- newBook.AppendChild(newAuthor);
- root.AppendChild(newBook);
- //save
- document.Save(filePath);
- }
- //刪除節點
- private void button4_Click(object sender, EventArgs e)
- {
- XmlDocument document = new XmlDocument();
- document.Load(filePath);
- XmlElement root = document.DocumentElement;
- if (root.HasChildNodes)
- {
- XmlNode old = root.LastChild;
- root.RemoveChild(old);
- }
- document.Save(filePath);
- }
- //修改節點
- private void button5_Click(object sender, EventArgs e)
- {
- XmlDocument document = new XmlDocument();
- document.Load(filePath);
- XmlElement root = document.DocumentElement;
- if (root.HasChildNodes)
- {
- XmlNode oldNode = root.LastChild;
- XmlNode newNode = document.CreateElement("Book");
- root.ReplaceChild(newNode, oldNode);
- }
- document.Save(filePath);
- }
- //查詢節點
- private void button6_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- XmlDocument document = new XmlDocument();
- document.Load(filePath);
- XmlNodeList list = document.GetElementsByTagName("book");//要查詢的的節點名
- foreach (XmlNode node in list)
- {
- listBox1.Items.Add(node.Name);
- }
- }
- }
- }
using System; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Xml; namespace Update { public partial class Form1 : Form { string filePath = Directory.GetCurrentDirectory() + @"\uploadXML.xml"; public Form1() { InitializeComponent(); } //讀取xml private void button1_Click(object sender, EventArgs e) { if (File.Exists(filePath)) { XmlDocument document = new XmlDocument(); document.Load(filePath); XmlNode root = (XmlNode)document.DocumentElement; //循環展示成樹狀 xmlProcess(root,0); } } /* * 循環展示成樹狀 */ private void xmlProcess(XmlNode root,int i) { if (root == null) { return; } if (root is XmlElement) { listBox1.Items.Add(root.Name.PadLeft(root.Name.Length + i)); if (root.HasChildNodes) { xmlProcess(root.FirstChild,i+2); } if (root.NextSibling != null) { xmlProcess(root.NextSibling, i); } } else if (root is XmlText) { string text = ((XmlText)root).Value; listBox1.Items.Add(text.PadLeft(text.Length+i)); } } //讀取文本節點 private void button2_Click(object sender, EventArgs e) { XmlDocument document = new XmlDocument(); document.Load(filePath); XmlNode root = (XmlNode)document.DocumentElement; string values = root.InnerText; string values2 = root.InnerXml; string values3 = root.Value; MessageBox.Show(values); MessageBox.Show(values2); MessageBox.Show(values3); } //添加節點 private void button3_Click(object sender, EventArgs e) { XmlDocument document = new XmlDocument(); document.Load(filePath); XmlElement root = document.DocumentElement; //create node XmlElement newBook = document.CreateElement("book"); XmlElement newTitle = document.CreateElement("title"); XmlElement newAuthor = document.CreateElement("author"); //create text XmlText titleText = document.CreateTextNode("new title"); XmlText authorText = document.CreateTextNode("new author"); //insert newTitle.AppendChild(titleText); newAuthor.AppendChild(authorText); newBook.AppendChild(newTitle); newBook.AppendChild(newAuthor); root.AppendChild(newBook); //save document.Save(filePath); } //刪除節點 private void button4_Click(object sender, EventArgs e) { XmlDocument document = new XmlDocument(); document.Load(filePath); XmlElement root = document.DocumentElement; if (root.HasChildNodes) { XmlNode old = root.LastChild; root.RemoveChild(old); } document.Save(filePath); } //修改節點 private void button5_Click(object sender, EventArgs e) { XmlDocument document = new XmlDocument(); document.Load(filePath); XmlElement root = document.DocumentElement; if (root.HasChildNodes) { XmlNode oldNode = root.LastChild; XmlNode newNode = document.CreateElement("Book"); root.ReplaceChild(newNode, oldNode); } document.Save(filePath); } //查詢節點 private void button6_Click(object sender, EventArgs e) { listBox1.Items.Clear(); XmlDocument document = new XmlDocument(); document.Load(filePath); XmlNodeList list = document.GetElementsByTagName("book");//要查詢的的節點名 foreach (XmlNode node in list) { listBox1.Items.Add(node.Name); } } } }