基本信息
源码名称:C# 操作xml文件(创建、修改、保存等编辑操作)
源码大小:0.05M
文件格式:.rar
开发语言:C#
更新时间:2019-12-21
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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
}
}