WPF对于xml的简单操作(上)

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            XmlTextWriter writer = new XmlTextWriter("MyInfo.xml", System.Text.Encoding.UTF8);

            //使用自动缩进便于阅读

            writer.Formatting = Formatting.Indented;

            //XML声明

            writer.WriteStartDocument();

            //书写根元素

            writer.WriteStartElement("Root");

            //开始一个元素

            writer.WriteStartElement("Item ");

            //向先前创建的元素中添加一个属性

            writer.WriteAttributeString("id", "1");

            //添加子元素
writer.WriteElementString("X", "x1"); writer.WriteElementString("Y", "y1"); writer.WriteElementString("Z", "z1"); //关闭item元素 writer.WriteEndElement(); // 关闭元素 //在节点间添加一些空 writer.Close(); } //读取xml数据 private void button2_Click(object sender, RoutedEventArgs e) { string ExePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\MyInfo.xml"; try { // 1.创建一个XmlDocument类的对象 XmlDocument doc = new XmlDocument(); // 2.把你想要读取的xml文档加载进来 doc.Load(ExePath); // 3.读取你指定的节点 XmlNode xn = doc.SelectSingleNode("Root"); XmlNodeList lis = xn.ChildNodes; // 4.取节点中的值 String str = lis[1].InnerText; MessageBox.Show(str); } catch { MessageBox.Show("配置文件存在异常!"); } } //插入xml数据 private void button3_Click(object sender, RoutedEventArgs e) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("MyInfo.xml"); XmlNode root = xmlDoc.SelectSingleNode("Root");//查找父节点 XmlElement xe1 = xmlDoc.CreateElement("Item");//创建一个子节点 xe1.SetAttribute("id", "3");//设置该节点id属性 XmlElement xesub1 = xmlDoc.CreateElement("X"); xesub1.InnerText = "x3";//设置文本节点 xe1.AppendChild(xesub1);//添加到<MyInfo>节点中 XmlElement xesub2 = xmlDoc.CreateElement("Y"); xesub2.InnerText = "y3"; xe1.AppendChild(xesub2); XmlElement xesub3 = xmlDoc.CreateElement("Z"); xesub3.InnerText = "z3"; xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到<bookstore>节点中 xmlDoc.Save("MyInfo.xml"); } //删除xml数据 private void button4_Click(object sender, RoutedEventArgs e) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("MyInfo.xml"); XmlNode xmlRoot = xmlDoc.SelectSingleNode("Root"); XmlNodeList xnl = xmlRoot.ChildNodes; foreach (XmlNode xn in xnl) { XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("id") == "3") { xmlRoot.RemoveChild(xn); } } xmlDoc.Save("MyInfo.xml"); } //修改xml数据 private void button5_Click(object sender, RoutedEventArgs e) {
} //显示xml数据 private void button6_Click(object sender, RoutedEventArgs e) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("MyInfo.xml"); XmlNode xn = xmlDoc.SelectSingleNode("Root");
XmlNodeList xnl = xn.ChildNodes;
string str = ""; string str1 = ""; string str2 = ""; foreach (XmlNode xnf in xnl)
{ XmlElement xe = (XmlElement)xnf; XmlNodeList xnf1 = xe.ChildNodes; str = xnf1[0].InnerText; str1 = xnf1[1].InnerText; str2 = xnf1[2].InnerText; MessageBox.Show("X:" + str + "\n" + "Y:" + str1 + "\n" + "Z:" + str2); } MessageBox.Show("X:" + str + "\n" + "Y:" + str1 + "\n" + "Z:"+str2); }

原代码是在网上搜的,我按照自己想要的效果修修改改,至少使得自己能看的一目了然,快要下班赶紧上传上来,修改部分有空补上

WPF对于xml的简单操作(上)WPF对于xml的简单操作(上)

你可能感兴趣的:(xml)