lijinchang

导航

.net 对XML实例

管理窗体如下

操作的xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<Books>
  <Book Author="利亚南" NO="002">
    <Title>系统自动化控制</Title>
    <KM>计算机</KM>
    <Price>120</Price>
  </Book>
  <Book Author="李金昌" NO="003">
    <Title>计算机原理</Title>
    <KM>计算机科学</KM>
    <Price>110</Price>
  </Book>
  <Book Author="李金昌" NO="003">
    <Title>计算机原理</Title>
    <KM>计算机科学</KM>
    <Price>110</Price>
  </Book>
  <Book Author="李金昌" NO="003">
    <Title>计算机原理</Title>
    <KM>计算机科学</KM>
    <Price>110</Price>
  </Book>
  <Book Author="李金昌" NO="003">
    <Title>计算机原理</Title>
    <KM>计算机科学</KM>
    <Price>110</Price>
  </Book>
  <Book Author="李金昌" NO="003">
    <Title>计算机原理</Title>
    <KM>计算机科学</KM>
    <Price>110</Price>
  </Book>
</Books>

控制代码如下:

private string path = @"D:\My Documents\WindowsApplication1\WindowsApplication1\b.xml";
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //if (!File.Exists(@"D:\My Documents\WindowsApplic\b.xml"))
            //{
            //    File.CreateText(@"D:\My Documents\WindowsApplic\b.xml");
            //}
            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldcl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            xmldoc.AppendChild(xmldcl);
            XmlElement xe = xmldoc.CreateElement("", "Books", "");
            xmldoc.AppendChild(xe);
            xmldoc.Save(@"D:\My Documents\WindowsApplication1\WindowsApplication1\b.xml");
        }
        /// <summary>
        /// 添加事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(@"D:\My Documents\WindowsApplication1\WindowsApplication1\b.xml");
            XmlNode root = xmldoc.SelectSingleNode("Books");
            XmlElement xmlnode = xmldoc.CreateElement("Book");
            XmlElement xe = xmlnode;
            xe.SetAttribute("Author", TxtAuthor.Text);
            xe.SetAttribute("NO", TxtNo.Text);
            XmlElement xe1 = xmldoc.CreateElement("Title");
            xe1.InnerText = TxtTitle.Text;
            xmlnode.AppendChild(xe1);
            XmlElement xe2 = xmldoc.CreateElement("KM");
            xe2.InnerText = TxtKM.Text;
            xmlnode.AppendChild(xe2);
            XmlElement xe3 = xmldoc.CreateElement("Price");
            xe3.InnerText = TxtPrice.Text;
            xmlnode.AppendChild(xe3);
            XmlNode xn = (XmlNode)xmlnode;
            root.AppendChild(xn);
            xmldoc.Save(@"D:\My Documents\WindowsApplication1\WindowsApplication1\b.xml");
        }
        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(@"D:\My Documents\WindowsApplication1\WindowsApplication1\b.xml");
            return;
        }
        /// <summary>
        /// 查看事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
           string author= TxtAuthor.Text;//作者
           XmlDocument xmldoc = new XmlDocument();
           xmldoc.Load(@"D:\My Documents\WindowsApplication1\WindowsApplication1\b.xml");
           XmlNode xn = xmldoc.FirstChild.NextSibling;
           XmlNodeList nl = xn.ChildNodes;
           foreach (XmlNode xni in nl)
           {
               XmlElement xe=(XmlElement) xni;
               if (xe.GetAttribute("Author") == TxtAuthor.Text)
               {
                   string str = "作者:" + xe.GetAttribute("Author") + ",编号:" + xe.GetAttribute("NO") + ",书名:" + xe.ChildNodes[0].InnerText + ",科目:" + xe.ChildNodes[1].InnerText+",价格:"+xe.ChildNodes[2].InnerText;
                   MessageBox.Show(str);
                   //xe.ParentNode.RemoveChild(xe);
               }
           }
           xmldoc.Save(@"D:\My Documents\WindowsApplication1\WindowsApplication1\b.xml");

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string author = TxtAuthor.Text;//作者
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(@"D:\My Documents\WindowsApplication1\WindowsApplication1\b.xml");
            XmlNode xn = xmldoc.FirstChild.NextSibling;
            XmlNodeList nl = xn.ChildNodes;
            foreach (XmlNode xni in nl)
            {
                XmlElement xe = (XmlElement)xni;
                if (xe.GetAttribute("Author") == TxtAuthor.Text||xe.GetAttribute("N0")==TxtNo.Text)
                {
                    string str = "作者:" + xe.GetAttribute("Author") + ",编号:" + xe.GetAttribute("NO") + ",书名:" + xe.ChildNodes[0].InnerText + ",科目:" + xe.ChildNodes[1].InnerText + ",价格:" + xe.ChildNodes[2].InnerText;
                    MessageBox.Show(str);
                    xe.ParentNode.RemoveChild(xe);
                }
            }
            xmldoc.Save(@"D:\My Documents\WindowsApplication1\WindowsApplication1\b.xml");
        }
        /// <summary>
        /// 修改节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(path);
            XmlNode xn1 = xmldoc.FirstChild.NextSibling;
            XmlNodeList nl = xn1.ChildNodes;
            foreach(XmlNode xn in nl)
            {
                if (((XmlElement)xn).GetAttribute("Author") == TxtAuthor.Text || ((XmlElement)xn).GetAttribute("NO") == TxtNo.Text)
                {
                    ((XmlElement)xn).SetAttribute("Author",TxtAuthor.Text);
                    ((XmlElement)xn).SetAttribute("NO",TxtNo.Text);
                    xn.ChildNodes[0].InnerText = TxtTitle.Text;
                    xn.ChildNodes[1].InnerText = TxtKM.Text;
                    xn.ChildNodes[2].InnerText = TxtPrice.Text;
                }
            }
            xmldoc.Save(path);
        }

 

posted on 2010-12-30 13:20  lijinchang  阅读(386)  评论(0编辑  收藏  举报