实例介绍
【实例简介】
【实例截图】
【核心代码】
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.IO; using System.Runtime.InteropServices; using System.Collections; using System.Xml; namespace FileOperate { public partial class Form1 : Form { public Form1() { dtbfinal.Columns.Add("id", typeof(string)); InitializeComponent(); } #region 变量声明区 public string str = "";//该变量保存INI文件所在的具体物理位置 public string strOne = ""; string filePath = "";//文件路径 OpenFileDialog ofd = new OpenFileDialog();//对话框 SaveFileDialog save = new SaveFileDialog(); DataTable dtb = new DataTable();//全局表 DataTable dtbfinal = new DataTable();//全局表 TextBox textbox = new TextBox(); List<string> temp = new List<string>(); List<string> listNew = new List<string>();//全局 List List<string> list = new List<string>(); List<string> listNew1 = new List<string>(); #endregion #region 创建节点 public void CreateNode(XmlDocument xmldoc, XmlNode parentnode, string name, string value) { XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, name, null); node.InnerText = value; parentnode.AppendChild(node); } #endregion #region 打开 private void button2_Click(object sender, EventArgs e) { dtb.Clear(); listBox1.Items.Clear(); ofd.Multiselect = false; ofd.Title = "选择XML文件"; ofd.Filter = "XML文件(*.XML)|*.XML"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { listBox1.Items.Clear(); dtbfinal.Clear(); if (ofd.FileName.ToString().Trim().Equals("")) { return; } else { filePath = ofd.FileName; str = filePath; if (File.Exists(filePath)) { try { List<Model> ModeList = new List<Model>(); XmlDocument doc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true;//忽略文档里面的注释 using (XmlReader reader = XmlReader.Create(str, settings)) { doc.Load(reader); XmlNode xn = doc.SelectSingleNode("ConnectString"); // 得到根节点的所有子节点 XmlNodeList xnl = xn.ChildNodes; int i = 0; foreach (XmlNode xn1 in xnl) { DataRow dr = dtbfinal.NewRow(); Model Model = new Model(); // 将节点转换为元素,便于得到节点的属性值 XmlElement xe = (XmlElement)xn1; // 得到Type和ISBN两个属性的属性值 Model.Num = xe.GetAttribute("ISBN").ToString(); ModeList.Add(Model); dr[0] = Model.Num; this.dtbfinal.Rows.Add(dr); } } ModeList.Add(new Model()); foreach (DataRow row in dtbfinal.Rows) { string value = row[0].ToString(); listBox1.Items.Add(value); } int x; string[] arrayA = new string[dtbfinal.Rows.Count]; for (x = 0; x < dtbfinal.Rows.Count; x ) { DataRow dr2 = dtbfinal.Rows[x]; arrayA[x] = Convert.ToString(dr2["id"]); TextBox textbox = new TextBox(); textbox.Name = "tb_" x; textbox.Text = dtbfinal.Rows[x]["id"].ToString(); textbox.Location = new Point(12, 15 x * 30); this.groupBox2.Controls.Add(textbox); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } } #endregion #region 上移 private void button3_Click(object sender, EventArgs e) { int index = listBox1.SelectedIndex; if (index == -1)//没选中 { return; } else if(index > 0) { string str = listBox1.Items[index].ToString(); listBox1.Items.RemoveAt(index); listBox1.Items.Insert(index - 1, str); listBox1.SelectedIndex = index - 1; } } #endregion #region 下移 private void button4_Click(object sender, EventArgs e) { int index = listBox1.SelectedIndex; if (index == -1) //没选中 { return; } else if (index<listBox1.Items.Count-1) { string str = listBox1.Items[index].ToString(); listBox1.Items.RemoveAt(index); listBox1.Items.Insert(index 1, str); listBox1.SelectedIndex = index 1; } } #endregion #region 保存 private void button5_Click(object sender, EventArgs e) { listNew1.Clear(); XmlDocument xmldoc = new XmlDocument(); XmlNode node; node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null); xmldoc.AppendChild(node); XmlNode root = xmldoc.CreateElement("ConnectString"); xmldoc.AppendChild(root); foreach (Control c in groupBox2.Controls) { if (c is TextBox) { listNew1.Add(c.Text.ToString()); } } foreach (var item in listNew1) { XmlElement Item = xmldoc.CreateElement("book"); string name = "ISBN"; Item.SetAttribute(name, item.ToString()); root.AppendChild(Item); } try { xmldoc.Save(filePath); MessageBox.Show("修改成功!"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region 创建 private void button6_Click(object sender, EventArgs e) { try { list.Clear(); ofd.Filter = "文本文件(*.txt*)|*.txt*"; ofd.FilterIndex = 1; ofd.RestoreDirectory = true; if (ofd.ShowDialog() == DialogResult.OK) { string localFilePath = ofd.FileName.ToString(); //获得文件路径 //string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") 1); //获取文件名,不带路径 string[] lines = File.ReadAllLines(localFilePath, System.Text.Encoding.UTF8); XmlDocument xmldoc = new XmlDocument(); XmlNode node; node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null); xmldoc.AppendChild(node); XmlNode root = xmldoc.CreateElement("ConnectString"); xmldoc.AppendChild(root); foreach (var item in lines) { listNew.Add(item.ToString()); } foreach (var item in listNew) { XmlElement Item = xmldoc.CreateElement("book"); string name = "ISBN"; Item.SetAttribute(name, item.ToString()); root.AppendChild(Item); } save.Filter = "文件(*.xml*)|*.xml*"; save.FilterIndex = 1; save.RestoreDirectory = true; if (save.ShowDialog() == DialogResult.OK) { localFilePath = save.FileName.ToString(); xmldoc.Save(localFilePath); MessageBox.Show("创建XML文件成功!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region 获取 private void button1_Click_1(object sender, EventArgs e) { temp.Clear(); foreach (string b in listBox1.Items) { temp.Add(b); } } #endregion } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)