解析XML【C#】
?
XML节点(Node)与元素(Element): 节点可以是一个元素节点、属性节点、文本节点、注释节点或任何其他DOM中定义的节点类型; 元素(是一种节点)可包含属性,其他元素或文本。如果元素含有文本,则在文本节点中表示该文本。注意:文本永远存储在文本节点中。即使最简单的元素节点也拥有文本节点。举例:在<year>2005</year>中,有一个元素节点<year>,同时此节点之下存在一个文本节点,其中含有文本“2005”。 1.XML元素 7.XML解析代码
<span style="font-family:KaiTi_GB2312;font-size:18px;">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); } } } } </span> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |