Data Binding Example - CSusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Xml; using System.IO; namespace csdemo2008.Wpf_Spring.XmlWindow { /// <summary> /// XmlWindow.xaml 的交互逻辑 /// </summary> public partial class XmlWindow : Window { public XmlWindow() { InitializeComponent(); } private void btnClose_Click(object sender,RoutedEventArgs e) { this.Close(); } XmlDocument doc = new XmlDocument(); // 写入XML文件 private void btnWrite_Click(object sender,RoutedEventArgs e) { string nodeText = tbNode.Text.Trim(); try { doc.Load("God.xml"); } catch { XmlTextWriter writer = new XmlTextWriter("God.xml",Encoding.UTF8); writer.WriteStartElement("rootNode"); writer.WriteStartElement("innerNode"); writer.WriteAttributeString("name","InnerNode"); writer.WriteStartElement("childNode"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.Close(); doc.Load("God.xml"); } bool isExist = false; foreach (XmlElement element in doc.DocumentElement) { // 这里读入的element是doc的第一级子节点,也就是innerNode if (element.Name == "innerNode" && element.HasChildNodes) { foreach (XmlNode node in element.ChildNodes) { if (node.Name == "childNode" && node.InnerText != nodeText) { node.InnerText = nodeText; } isExist = true; } } else { doc.RemoveChild(element); } } if (isExist == false) { XmlElement elementRoot = doc.CreateElement("innerNode"); XmlElement elementInner = doc.CreateElement("childNode"); elementInner.InnerText = "『峻之岭峰』- http://www.cnblogs.com/sitemanager/"; elementRoot.AppendChild(elementInner); // 注意,这里是加在RootElement之内的! doc.DocumentElement.AppendChild(elementRoot); } FileInfo info = new FileInfo("God.xml"); if (info.Attributes == FileAttributes.ReadOnly) { info.Attributes = FileAttributes.Normal; } doc.Save("God.xml"); info.Attributes = FileAttributes.ReadOnly; tbNode.Text = null; } // 读取XML文件 private void btnRead_Click(object sender,RoutedEventArgs e) { try { doc.Load("God.xml"); } catch { MessageBox.Show("没有这个XML文件!请先点击写入文件来创建它!"); } if (tbNode.Text.Trim() == "") { tblDisplay.Text += doc.InnerXml + "\n"; } else { foreach (XmlElement element in doc.DocumentElement) { if (element.HasChildNodes) { foreach (XmlNode node in element.ChildNodes) { if (node.InnerText == tbNode.Text.Trim()) { tblDisplay.Text += node.InnerText + "\n"; } else { tblDisplay.Text += "没有对应节点!\n"; } } } else { tblDisplay.Text += element.InnerXml + "\n"; } } } tbNode.Text = null; } private void button2_Click(object sender,RoutedEventArgs e) { tbNode.Text = null; tblDisplay.Text = null; } private void button1_Click(object sender,RoutedEventArgs e) { try { FileInfo info = new FileInfo("God.xml"); info.Attributes = FileAttributes.Normal; info.Delete(); } catch(Exception ee) { MessageBox.Show(ee.Message); } } } }