XML文件的一些操作
XML 是被设计用来传输和存储数据的,
XML 必须含有且仅有一个 根节点元素(没有根节点会报错)
源码下载 http://pan.baidu.com/s/1ge2lpM7
好了,我们 先看一个 XML 文件(Cartoon.xml)
<?xml version="1.0" encoding="utf-8"?>
<Cartoon>
<Character Company="DC">
<Name>超人</Name>
<Age></Age>
</Character>
<Character Company="Marve">
<Name>雷神</Name>
<Age></Age>
</Character>
</Cartoon>
第二个 XML 文件 (Attribute.xml)
<?xml version="1.0" encoding="utf-8"?>
<Cartoon>
<Character Name="超人" Company="DC" Age="" />
<Character Name="雷神" Company="Marve" Age="" />
</Cartoon>
既然是小型存储文件,就避免不了增 删 改 查.
全部 方法代码 如下 (用了两种方法 增 删 改 查).
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq; namespace XML_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} //创建XML
private void btn_Create_XML_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
XmlDeclaration describe = doc.CreateXmlDeclaration("1.0", "utf-8", null); // 创建 描述信息
doc.AppendChild(describe); // 写入描述 XmlElement cartoon = doc.CreateElement("Cartoon"); //创建根节点
doc.AppendChild(cartoon); // 写入根节点 //子节点1
XmlElement character1 = doc.CreateElement("Character");
character1.SetAttribute("Company", "DC"); XmlElement name1 = doc.CreateElement("Name");
name1.InnerText = "超人";
character1.AppendChild(name1); XmlElement age1 = doc.CreateElement("Age");
age1.InnerText = "";
character1.AppendChild(age1);
cartoon.AppendChild(character1); //子节点写入根节点 //子节点2
XmlElement character2 = doc.CreateElement("Character");
character2.SetAttribute("Company", "Marve"); XmlElement name2 = doc.CreateElement("Name");
name2.InnerText = "雷神";
character2.AppendChild(name2); XmlElement age2 = doc.CreateElement("Age");
age2.InnerText = "";
character2.AppendChild(age2);
cartoon.AppendChild(character2); //子节点写入根节点 doc.Save(@"C:\Cartoon.xml");
Console.WriteLine("写入成功!");
} //查询XML
private void btn_Search_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Cartoon.xml"); //获取根节点
XmlElement superHeroes = doc.DocumentElement; // 获取子节点的集合
XmlNodeList heroes = superHeroes.ChildNodes; foreach (XmlNode hero in heroes)
{
Console.WriteLine(hero.SelectSingleNode("Name").InnerText + ":" +
hero.SelectSingleNode("Age").InnerText);
}
} //修改XML
private void btn_Edit_Click(object sender, EventArgs e)
{ XmlDocument doc = new XmlDocument();
var file = @"C:\Cartoon.xml";
if (File.Exists(file))
{
doc.Load(file);
//获取根节点
XmlNode root = doc.DocumentElement;
//获取节点的所有子节点
XmlNodeList characters = root.ChildNodes;
foreach (XmlNode hero in characters)
{
string node = hero.SelectSingleNode("Name").InnerText;
if (node == "超人")
{
XmlNode age = hero.SelectSingleNode("Age");
age.InnerText = "";
doc.Save(file);
Console.WriteLine("修改成功!");
}
}
}
else
{
Console.WriteLine("文件不存在!");
}
} //修改XML(追加)
private void btn_Edit_Add_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
var file = @"C:\Cartoon.xml";
if (File.Exists(file))
{
doc.Load(file);
//获取根节点
XmlElement cartoonNew = doc.DocumentElement;
XmlElement characterNew = doc.CreateElement("Character");
characterNew.SetAttribute("Company", "DC"); XmlElement nameNew = doc.CreateElement("Name");
nameNew.InnerText = "Doctor Manhattan";
characterNew.AppendChild(nameNew); XmlElement ageNew = doc.CreateElement("Age");
ageNew.InnerText = "";
characterNew.AppendChild(ageNew);
cartoonNew.AppendChild(characterNew); //子节点写入根节点
doc.Save(file);
Console.WriteLine("添加成功!"); }
else
{
Console.WriteLine("文件不存在!");
}
} //删除XML
private void btn_Del_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
var file = @"C:\Cartoon.xml";
doc.Load(file);
XmlNode root = doc.SelectSingleNode("Cartoon");
//root.RemoveAll(); //删除根节点下的 所有节点
foreach (XmlNode xn in root.ChildNodes)
{
if (xn.FirstChild.InnerText == "超人")
{
root.RemoveChild(xn);
}
} doc.Save(file);
Console.WriteLine("删除成功");
} //创建XML2
private void btn_Create_XML2_Click(object sender, EventArgs e)
{
XElement xel = new XElement("Cartoon",
new XElement("Character",
new XAttribute("Company", "DC"),
new XElement("Name", "超人"),
new XElement("Age", "")),
new XElement("Character",
new XAttribute("Company", "Marve"),
new XElement("Name", "雷神"),
new XElement("Age", ""))
);
XDocument xdoc = new XDocument(xel);
xdoc.Save(@"c:\Cartoon2.xml");
} //查询XML2
private void btn_Search2_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Cartoon2.xml");
XmlNodeList characterList = doc.SelectNodes("/Character");
foreach (XmlNode item in characterList)
{
Console.WriteLine(item.SelectSingleNode("Name").InnerText+":"+
item.SelectSingleNode("Age").InnerText);
}
} //修改XML2
private void btn_Edit2_Click(object sender, EventArgs e)
{
XElement doc = XElement.Load(@"C:\Cartoon2.xml");
XElement heroes = (from p in doc.Elements() where p.Element("Name").Value == "超人" select p).FirstOrDefault();
if (heroes != null)
{
heroes.Element("Age").Value = "";
doc.Save(@"C:\Cartoon2.xml");
Console.WriteLine("添加成功!");
} } //删除XML2
private void btn_Del2_Click(object sender, EventArgs e)
{
XElement doc = XElement.Load(@"C:\Cartoon2.xml");
XElement xchild = doc.Descendants("Name").Single(p => p.Value.Equals("超人"));
XElement nameNode = xchild.Parent;
nameNode.Remove();
doc.Save(@"C:\Cartoon2.xml");
} //创建多属性XML
private void btn_XML_Attribute_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
XmlDeclaration describe = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(describe); XmlElement cartoon = doc.CreateElement("Cartoon");
doc.AppendChild(cartoon); XmlElement character1 = doc.CreateElement("Character");
character1.SetAttribute("Name", "超人");
character1.SetAttribute("Company","DC");
character1.SetAttribute("Age", "");
cartoon.AppendChild(character1); XmlElement character2 = doc.CreateElement("Character");
character2.SetAttribute("Name", "雷神");
character2.SetAttribute("Company", "Marve");
character2.SetAttribute("Age", "");
cartoon.AppendChild(character2); doc.Save(@"C:\Attribute.xml");
Console.WriteLine("写入成功!");
} //查询多属性XML
private void btn_Search_Attribute_Click(object sender, EventArgs e)
{
var file = @"C:\Attribute.xml";
XmlDocument doc = new XmlDocument();
doc.Load(file);
XmlNodeList xnl = doc.SelectNodes("/Cartoon/Character");
foreach (XmlNode item in xnl)
{
Console.WriteLine(item.Attributes["Name"].Value );
}
} //修改多属性XML
private void btn_Edit_Attribute_Click(object sender, EventArgs e)
{
var file = @"C:\Attribute.xml";
XElement doc = XElement.Load(file);
XElement heroes = (from p in doc.Elements() where p.Attribute("Name").Value == "超人" select p).FirstOrDefault();
if (heroes != null)
{
heroes.Attribute("Age").Value = "";
doc.Save(file);
Console.WriteLine("修改成功!");
} } //删除多属性XML
private void btn_Del_Attribute_Click(object sender, EventArgs e)
{
var file = @"C:\Attribute.xml";
XElement xl = XDocument.Load(file).Root;
xl.Elements("Character").Where(el => el.Attribute("Name").Value.Equals("超人")).Remove();
xl.Save(file);
Console.WriteLine("修改成功!");
} //删除多属性XML2
private void btn_Del_Attribute2_Click(object sender, EventArgs e)
{
var file = @"C:\Attribute.xml";
XmlDocument doc = new XmlDocument();
doc.Load(file);
XmlElement root = doc.DocumentElement;
XmlNodeList element = doc.SelectNodes("/Cartoon/Character");
foreach (XmlNode item in element)
{
Console.WriteLine(item.Attributes["Name"].Value);
if (item.Attributes["Name"].Value == "超人")
{
root.RemoveChild(item);
}
}
doc.Save(file);
}
}
}
窗体设计代码 (如果不想拖控件,可以复制过去)
namespace XML_Demo
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn_Create_XML = new System.Windows.Forms.Button();
this.btn_Create_XML2 = new System.Windows.Forms.Button();
this.btn_Search = new System.Windows.Forms.Button();
this.btn_Edit_Add = new System.Windows.Forms.Button();
this.btn_Edit = new System.Windows.Forms.Button();
this.btn_Del = new System.Windows.Forms.Button();
this.btn_Search2 = new System.Windows.Forms.Button();
this.btn_Edit2 = new System.Windows.Forms.Button();
this.btn_Del2 = new System.Windows.Forms.Button();
this.btn_XML_Attribute = new System.Windows.Forms.Button();
this.btn_Search_Attribute = new System.Windows.Forms.Button();
this.btn_Edit_Attribute = new System.Windows.Forms.Button();
this.btn_Del_Attribute = new System.Windows.Forms.Button();
this.btn_Del_Attribute2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btn_Create_XML
//
this.btn_Create_XML.Location = new System.Drawing.Point(, );
this.btn_Create_XML.Name = "btn_Create_XML";
this.btn_Create_XML.Size = new System.Drawing.Size(, );
this.btn_Create_XML.TabIndex = ;
this.btn_Create_XML.Text = "创建XML";
this.btn_Create_XML.UseVisualStyleBackColor = true;
this.btn_Create_XML.Click += new System.EventHandler(this.btn_Create_XML_Click);
//
// btn_Create_XML2
//
this.btn_Create_XML2.Location = new System.Drawing.Point(, );
this.btn_Create_XML2.Name = "btn_Create_XML2";
this.btn_Create_XML2.Size = new System.Drawing.Size(, );
this.btn_Create_XML2.TabIndex = ;
this.btn_Create_XML2.Text = "创建XML2";
this.btn_Create_XML2.UseVisualStyleBackColor = true;
this.btn_Create_XML2.Click += new System.EventHandler(this.btn_Create_XML2_Click);
//
// btn_Search
//
this.btn_Search.Location = new System.Drawing.Point(, );
this.btn_Search.Name = "btn_Search";
this.btn_Search.Size = new System.Drawing.Size(, );
this.btn_Search.TabIndex = ;
this.btn_Search.Text = "查询XML";
this.btn_Search.UseVisualStyleBackColor = true;
this.btn_Search.Click += new System.EventHandler(this.btn_Search_Click);
//
// btn_Edit_Add
//
this.btn_Edit_Add.Location = new System.Drawing.Point(, );
this.btn_Edit_Add.Name = "btn_Edit_Add";
this.btn_Edit_Add.Size = new System.Drawing.Size(, );
this.btn_Edit_Add.TabIndex = ;
this.btn_Edit_Add.Text = "修改XML(追加)";
this.btn_Edit_Add.UseVisualStyleBackColor = true;
this.btn_Edit_Add.Click += new System.EventHandler(this.btn_Edit_Add_Click);
//
// btn_Edit
//
this.btn_Edit.Location = new System.Drawing.Point(, );
this.btn_Edit.Name = "btn_Edit";
this.btn_Edit.Size = new System.Drawing.Size(, );
this.btn_Edit.TabIndex = ;
this.btn_Edit.Text = "修改XML";
this.btn_Edit.UseVisualStyleBackColor = true;
this.btn_Edit.Click += new System.EventHandler(this.btn_Edit_Click);
//
// btn_Del
//
this.btn_Del.Location = new System.Drawing.Point(, );
this.btn_Del.Name = "btn_Del";
this.btn_Del.Size = new System.Drawing.Size(, );
this.btn_Del.TabIndex = ;
this.btn_Del.Text = "删除XML";
this.btn_Del.UseVisualStyleBackColor = true;
this.btn_Del.Click += new System.EventHandler(this.btn_Del_Click);
//
// btn_Search2
//
this.btn_Search2.Location = new System.Drawing.Point(, );
this.btn_Search2.Name = "btn_Search2";
this.btn_Search2.Size = new System.Drawing.Size(, );
this.btn_Search2.TabIndex = ;
this.btn_Search2.Text = "查询XML2";
this.btn_Search2.UseVisualStyleBackColor = true;
this.btn_Search2.Click += new System.EventHandler(this.btn_Search2_Click);
//
// btn_Edit2
//
this.btn_Edit2.Location = new System.Drawing.Point(, );
this.btn_Edit2.Name = "btn_Edit2";
this.btn_Edit2.Size = new System.Drawing.Size(, );
this.btn_Edit2.TabIndex = ;
this.btn_Edit2.Text = "修改XML2";
this.btn_Edit2.UseVisualStyleBackColor = true;
this.btn_Edit2.Click += new System.EventHandler(this.btn_Edit2_Click);
//
// btn_Del2
//
this.btn_Del2.Location = new System.Drawing.Point(, );
this.btn_Del2.Name = "btn_Del2";
this.btn_Del2.Size = new System.Drawing.Size(, );
this.btn_Del2.TabIndex = ;
this.btn_Del2.Text = "删除XML2";
this.btn_Del2.UseVisualStyleBackColor = true;
this.btn_Del2.Click += new System.EventHandler(this.btn_Del2_Click);
//
// btn_XML_Attribute
//
this.btn_XML_Attribute.Location = new System.Drawing.Point(, );
this.btn_XML_Attribute.Name = "btn_XML_Attribute";
this.btn_XML_Attribute.Size = new System.Drawing.Size(, );
this.btn_XML_Attribute.TabIndex = ;
this.btn_XML_Attribute.Text = "创建多属性XML";
this.btn_XML_Attribute.UseVisualStyleBackColor = true;
this.btn_XML_Attribute.Click += new System.EventHandler(this.btn_XML_Attribute_Click);
//
// btn_Search_Attribute
//
this.btn_Search_Attribute.Location = new System.Drawing.Point(, );
this.btn_Search_Attribute.Name = "btn_Search_Attribute";
this.btn_Search_Attribute.Size = new System.Drawing.Size(, );
this.btn_Search_Attribute.TabIndex = ;
this.btn_Search_Attribute.Text = "查询多属性XML";
this.btn_Search_Attribute.UseVisualStyleBackColor = true;
this.btn_Search_Attribute.Click += new System.EventHandler(this.btn_Search_Attribute_Click);
//
// btn_Edit_Attribute
//
this.btn_Edit_Attribute.Location = new System.Drawing.Point(, );
this.btn_Edit_Attribute.Name = "btn_Edit_Attribute";
this.btn_Edit_Attribute.Size = new System.Drawing.Size(, );
this.btn_Edit_Attribute.TabIndex = ;
this.btn_Edit_Attribute.Text = "修改多属性XML";
this.btn_Edit_Attribute.UseVisualStyleBackColor = true;
this.btn_Edit_Attribute.Click += new System.EventHandler(this.btn_Edit_Attribute_Click);
//
// btn_Del_Attribute
//
this.btn_Del_Attribute.Location = new System.Drawing.Point(, );
this.btn_Del_Attribute.Name = "btn_Del_Attribute";
this.btn_Del_Attribute.Size = new System.Drawing.Size(, );
this.btn_Del_Attribute.TabIndex = ;
this.btn_Del_Attribute.Text = "删除多属性XML";
this.btn_Del_Attribute.UseVisualStyleBackColor = true;
this.btn_Del_Attribute.Click += new System.EventHandler(this.btn_Del_Attribute_Click);
//
// btn_Del_Attribute2
//
this.btn_Del_Attribute2.Location = new System.Drawing.Point(, );
this.btn_Del_Attribute2.Name = "btn_Del_Attribute2";
this.btn_Del_Attribute2.Size = new System.Drawing.Size(, );
this.btn_Del_Attribute2.TabIndex = ;
this.btn_Del_Attribute2.Text = "删除多属性XML2";
this.btn_Del_Attribute2.UseVisualStyleBackColor = true;
this.btn_Del_Attribute2.Click += new System.EventHandler(this.btn_Del_Attribute2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.btn_Del_Attribute2);
this.Controls.Add(this.btn_Del_Attribute);
this.Controls.Add(this.btn_Edit_Attribute);
this.Controls.Add(this.btn_Search_Attribute);
this.Controls.Add(this.btn_XML_Attribute);
this.Controls.Add(this.btn_Del2);
this.Controls.Add(this.btn_Edit2);
this.Controls.Add(this.btn_Search2);
this.Controls.Add(this.btn_Del);
this.Controls.Add(this.btn_Edit);
this.Controls.Add(this.btn_Edit_Add);
this.Controls.Add(this.btn_Search);
this.Controls.Add(this.btn_Create_XML2);
this.Controls.Add(this.btn_Create_XML);
this.Name = "Form1";
this.Text = "XML文件的 增删改查";
this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button btn_Create_XML;
private System.Windows.Forms.Button btn_Create_XML2;
private System.Windows.Forms.Button btn_Search;
private System.Windows.Forms.Button btn_Edit_Add;
private System.Windows.Forms.Button btn_Edit;
private System.Windows.Forms.Button btn_Del;
private System.Windows.Forms.Button btn_Search2;
private System.Windows.Forms.Button btn_Edit2;
private System.Windows.Forms.Button btn_Del2;
private System.Windows.Forms.Button btn_XML_Attribute;
private System.Windows.Forms.Button btn_Search_Attribute;
private System.Windows.Forms.Button btn_Edit_Attribute;
private System.Windows.Forms.Button btn_Del_Attribute;
private System.Windows.Forms.Button btn_Del_Attribute2;
}
}
Form1.Designer.cs
XML文件的一些操作的更多相关文章
- php对xml文件进行CURD操作
XML是一种数据存储.交换.表达的标准: - 存储:优势在于半结构化,可以自定义schema,相比关系型二维表,不用遵循第一范式(可以有嵌套关系): - 交换:可以通过schema实现异构数据集成: ...
- 【JAVA使用XPath、DOM4J解析XML文件,实现对XML文件的CRUD操作】
一.简介 1.使用XPath可以快速精确定位指定的节点,以实现对XML文件的CRUD操作. 2.去网上下载一个“XPath帮助文档”,以便于查看语法等详细信息,最好是那种有很多实例的那种. 3.学习X ...
- 【转】C#对XML文件的各种操作实现方法
[转]C#对XML文件的各种操作实现方法 原文:http://www.jb51.net/article/35568.htm XML:Extensible Markup Language(可扩展标记语言 ...
- 【JAVA解析XML文件实现CRUD操作】
一.简介. 1.xml解析技术有两种:dom和sax 2.dom:Document Object Model,即文档对象模型,是W3C组织推荐的解析XML的一种方式. sax:Simple API f ...
- java代码用dom4j解析xml文件的简单操作
时间: 2016/02/17 目标:为telenor的ALU Femto接口写一个采集xml文件并解析出locationName标签里的值,然后更新到数据库中. 从网上搜了下,有四种常用的解析xml的 ...
- xml文件的读写操作
1.直接上代码:包含了xml文档的创建,读取xml文档,创建根节点,向根节点中添加子节点,保存xml文档----------先来张效果图: static void Main(string[] args ...
- Qt5 对xml文件常用的操作(读写,增删改查)
转自:https://blog.csdn.net/hpu11/article/details/80227093 项目配置 pro文件里面添加QT+=xml include <QtXml>, ...
- c#操作XML文件的通用方法
转载地址:http://www.studyofnet.com/news/36.html 原址没找到 sing System; using System.Data; using System.Confi ...
- Linq to XML操作XML文件
LINQ的类型 在MSDN官方文件中,LINQ分为几种类型: . LINQ to Objects(或称LINQ to Collection),这是LINQ的基本功能,针对集合对象进行查询处理,包括基本 ...
随机推荐
- django orm 操作符
__gt 大于__gte 大于等于__lt 小于__lte 小于等于__in__exact 精确等于 like 'aaa'__iexact 精确等于 忽略大小写 ilike 'aaa'__contai ...
- docker的操作
查询容器 docker ps 只能查询到正在运行的docker镜像: 如果添加上-a的选项,则会显示所有的(包括已经exit,未启动)的容器 基于一个镜像来构建(run)容器,并启动 docker ...
- HDU4348:To the moon
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227. ...
- 用OpenLayers开发地图应用
项目背景 最近有一个使用全球地图展示数据的项目,用地图展示数据本身没什么难度,但出于安全和保密的考虑,甲方单位要求项目不能连接外网,只能在内网使用,也就是说,我们不得不在内网中部署一个地图服务器,在这 ...
- fabric添加多主机ssh互信
最近折腾fabric,把服务器ssh互信用fabric写了一遍,单向互信,master可以无密码访问client,具体如下: 执行:fab -f ./copyrsa.py allsshkey 即可, ...
- local_irq_save 与 local_irq_restore
如果你要禁止所有的中断该怎么办? 在2.6内核中,可以通过下面两个函数中的其中任何一个关闭当前处理器上的所有中断处理,这两个函数定义在 <asm/system.h>中: void ...
- Gridview 每秒刷新数据
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx. ...
- 2016年第七届蓝桥杯国赛试题(JavaA组)
1.结果填空 (满分19分)2.结果填空 (满分35分)3.代码填空 (满分21分)4.程序设计(满分47分)5.程序设计(满分79分)6.程序设计(满分99分) 1.阶乘位数 9的阶乘等于:3628 ...
- HDU-6395 多校7 Sequence(除法分块+矩阵快速幂)
Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- element ui 修改默认样式
修改element ui默认的样式 如果要组件内全局修改 首先在浏览器里F12找到element默认的UI类名 找到要修改的默认类名以后 在文件中修改代码,重写属性 <style> .el ...