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文件的一些操作的更多相关文章

  1. php对xml文件进行CURD操作

    XML是一种数据存储.交换.表达的标准: - 存储:优势在于半结构化,可以自定义schema,相比关系型二维表,不用遵循第一范式(可以有嵌套关系): - 交换:可以通过schema实现异构数据集成: ...

  2. 【JAVA使用XPath、DOM4J解析XML文件,实现对XML文件的CRUD操作】

    一.简介 1.使用XPath可以快速精确定位指定的节点,以实现对XML文件的CRUD操作. 2.去网上下载一个“XPath帮助文档”,以便于查看语法等详细信息,最好是那种有很多实例的那种. 3.学习X ...

  3. 【转】C#对XML文件的各种操作实现方法

    [转]C#对XML文件的各种操作实现方法 原文:http://www.jb51.net/article/35568.htm XML:Extensible Markup Language(可扩展标记语言 ...

  4. 【JAVA解析XML文件实现CRUD操作】

    一.简介. 1.xml解析技术有两种:dom和sax 2.dom:Document Object Model,即文档对象模型,是W3C组织推荐的解析XML的一种方式. sax:Simple API f ...

  5. java代码用dom4j解析xml文件的简单操作

    时间: 2016/02/17 目标:为telenor的ALU Femto接口写一个采集xml文件并解析出locationName标签里的值,然后更新到数据库中. 从网上搜了下,有四种常用的解析xml的 ...

  6. xml文件的读写操作

    1.直接上代码:包含了xml文档的创建,读取xml文档,创建根节点,向根节点中添加子节点,保存xml文档----------先来张效果图: static void Main(string[] args ...

  7. Qt5 对xml文件常用的操作(读写,增删改查)

    转自:https://blog.csdn.net/hpu11/article/details/80227093 项目配置 pro文件里面添加QT+=xml include <QtXml>, ...

  8. c#操作XML文件的通用方法

    转载地址:http://www.studyofnet.com/news/36.html 原址没找到 sing System; using System.Data; using System.Confi ...

  9. Linq to XML操作XML文件

    LINQ的类型 在MSDN官方文件中,LINQ分为几种类型: . LINQ to Objects(或称LINQ to Collection),这是LINQ的基本功能,针对集合对象进行查询处理,包括基本 ...

随机推荐

  1. ACM学习历程—HDU 5317 RGCDQ (数论)

    Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...

  2. 监测GPU使用情况命令

    每2秒监测一次:watch -n 2 nvidia-smi

  3. BZOJ1568:[JSOI2008]Blue Mary开公司

    浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227.html 题目传送门:https://www.lydsy.com/JudgeOnline/proble ...

  4. 更换mysql数据目录后出现 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 的解决办法

    服务器上的mysql默认数据目录为/var/lib/mysql/,同时服务器的/空间不是很大,而近期又有大量的日志需要导入进行分析,时常搞得/的空间捉襟见肘,晚上一狠心就想把mysql的数据目录转移到 ...

  5. 基于zookeeper的MySQL主主负载均衡的简单实现

    1.先上原理图 2.说明 两个mysql采用主主同步的方式进行部署. 在安装mysql的服务器上安装客户端(目前是这么做,以后想在zookeeper扩展集成),客户端实时监控mysql应用的可用性,可 ...

  6. matlab下的caffe接口配置(Windows)

    本文基于大部分网上方法 http://blog.csdn.net/d5224/article/details/51916178,外加一点自己的个人实际配置经历,环境变量在配置后尽管显示正确并且重启多次 ...

  7. 【机器学习】随机森林RF

    随机森林(RF, RandomForest)包含多个决策树的分类器,并且其输出的类别是由个别树输出的类别的众数而定.通过自助法(boot-strap)重采样技术,不断生成训练样本和测试样本,由训练样本 ...

  8. linux 下消息队列发送后没有信息

    在使用消息队列时,调用 #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...

  9. 2018年第九届蓝桥杯国赛试题(JavaA组)

    1.结果填空 (满分13分)2.结果填空 (满分39分)3.代码填空 (满分27分)4.程序设计(满分45分)5.程序设计(满分71分)6.程序设计(满分105分) 1.标题:三角形面积 已知三角形三 ...

  10. CodeForces - 828C String Reconstruction 并查集(next跳)

    String Reconstruction Ivan had string s consisting of small English letters. However, his friend Jul ...