c# 操作xml题目



1、新建一个文本文件,命名为:projects.txt。
2、将后缀名改为projects.xml。 
3、用记事本编辑该文件。使用utf-8编码。内容如下:

<?xml version="1.0" encoding="GB2312"?>
<root>
  <projects>
    <project>
      <name>项目1</name>
      <url>http://baidu.com/</url>
      <sqltype>mysql</sqltype>
      <loginid>root</loginid>
      <password>pwd</password>
      <dt>2013-01-01</dt>
    </project>
    <project>
      <name>项目2</name>
      <url>http://google.com/</url>
      <sqltype>sql</sqltype>
      <loginid>root</loginid>
      <password>pwd</password>
      <dt>2013-10-10</dt>
    </project>
  </projects>
</root>

4、新建窗口应用程序,实现以下功能。放置一个菜单项(文件),包含三个子菜单项(打开xml,关闭,退出),
当点击“打开xml”时,程序直接打开程序当前目录下的projects.xml,解析出文件内容,存入Datatable中,
然后显示到窗口上。
当点击"关闭"时,清空窗口上的显示。
当点击“退出”时,关闭窗口。 

Rxml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Data;

namespace WindowsFormsApplication1
{
    class Rxml
    {
        private XmlDocument doc;
        public void createXML( string path) {
            doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null);
            doc.AppendChild(dec);

            XmlElement root = doc.CreateElement("root");
            doc.AppendChild(root);

            XmlNode projects = doc.CreateElement("projects");
            root.AppendChild(projects);

            doc.Save(path);
        }

        public void xmlload(string path) {
            doc = new XmlDocument();
            doc.Load(path);
        }

        public void addnode(string name,string url,string sqltype,string loginid,string password,string dt,string path ) {
            xmlload(path);
            XmlNode xmldocselect= doc.SelectSingleNode("/root/projects");
            XmlElement e1 = doc.CreateElement("project");

            XmlElement e2 = doc.CreateElement("name");
            e2.InnerText = name;
            e1.AppendChild(e2);

            XmlElement e3 = doc.CreateElement("url");
            e3.InnerText = url;
            e1.AppendChild(e3);

            XmlElement e4 = doc.CreateElement("sqltype");
            e4.InnerText = sqltype;
            e1.AppendChild(e4);

            XmlElement e5 = doc.CreateElement("loginid");
            e5.InnerText = loginid;
            e1.AppendChild(e5);

            XmlElement e6 = doc.CreateElement("password");
            e6.InnerText = password;
            e1.AppendChild(e6);

            XmlElement e7 = doc.CreateElement("dt");
            e7.InnerText = dt;
            e1.AppendChild(e7);

            xmldocselect.AppendChild(e1);
            doc.Save(path);
        }

        public DataTable readxml(string path)
        {
            //产生读取器。
            XmlTextReader read;
            //判断是否存在外部xml,如存在则读取外部的资源,如不存在则读取内部资源。
            if (File.Exists(path))
            {
                read = new XmlTextReader(path);
            }
            else
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                Stream sm = asm.GetManifestResourceStream("Tetris.setting.xml");
                read = new XmlTextReader(sm);
            }

            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Url", typeof(string));
            dt.Columns.Add("Sqltype", typeof(string));
            dt.Columns.Add("Loginid", typeof(string));
            dt.Columns.Add("Password", typeof(string));
            dt.Columns.Add("Dt", typeof(string));
            DataRow row = dt.NewRow();
          
            string key = "";
            try
            {

                while (read.Read())
                {
                    if (read.Name == "name")
                    {
                        key = read.ReadElementString().Trim();
                        string z = key.ToString().Trim();
                        row["Name"] = z;
                    }
                    else if (read.Name == "url")
                    {
                        key = read.ReadElementString().Trim();
                        string zz = key.ToString().Trim();
                        row["Url"] = zz;
                    }
                    else if (read.Name == "sqltype")
                    {
                        key = read.ReadElementString().Trim();
                        string zzz = key.ToString().Trim();
                        row["Sqltype"] = zzz;
                    }
                    else if (read.Name == "loginid")
                    {
                        key = read.ReadElementString().Trim();
                        string zzzz = key.ToString().Trim();
                        row["Loginid"] = zzzz;
                    }
                    else if (read.Name == "password")
                    {
                        key = read.ReadElementString().Trim();
                        string zzzzz = key.ToString().Trim();
                        row["Password"] = zzzzz;
                    }
                    else if (read.Name == "dt")
                    {
                        key = read.ReadElementString().Trim();
                        string zzzzzz = key.ToString().Trim();
                        row["Dt"] = zzzzzz;
                        dt.Rows.Add(row);
                        row = dt.NewRow();
                    }
                }
            }
            //异常处理。
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            //关闭读取器。
            finally
            {
                if (read != null)
                {
               
                    read.Close();
                }
            }
            return dt;
        }
    
    
    }
}

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        Rxml z = new Rxml();

        private void button1_Click(object sender, EventArgs e)
        {

            z.createXML(@"d:\bb.xml");
        }

        private void button2_Click(object sender, EventArgs e)
        {

            z.addnode("项目1", "http://baidu.com/", "mysql", "root", "pwd", "2013-01-01", @"d:\bb.xml");
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void 打开xmlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = z.readxml(@"d:\bb.xml");
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;

        }

    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值