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. 查询oracle 数据库 SQL语句执行情况

    1.查看总消耗时间最多的前10条SQL语句 select *  from (select v.sql_id,  v.child_number,  v.sql_text,  v.elapsed_time ...

  2. inteliji ---idea 如何创建scala程序配置

    首先创建一个新的工程: #####################################################################################

  3. C#中多线程中变量研究

    今天在知乎上看到一个问题[为什么在同一进程中创建不同线程,但线程各自的变量无法在线程间互相访问?].在多线程中,每个线程都是独立运行的,不同的线程有可能是同一段代码,但不会是同一作用域,所以不会共享. ...

  4. vue之axios+php+mysql

    博主原创,未经许可请勿转载 哦 1.axios配置请看上篇 2.mysql数据库编写,表名为area_list 3.json.php文件在notebeans中编写 <?php header('C ...

  5. bzoj 4319 Suffix reconstruction —— 贪心构造

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4319 思维还是不行...这样的构造都没思路... 首先,我们可以按 rank 的顺序从小到大 ...

  6. npm如何删除node_modules文件夹

    npm install rimraf -g 先安装删除工具,然后使用删除命令 rimraf node_modules

  7. emacs for OCaml

    (require 'cl) (require 'package) (add-to-list 'package-archives '("melpa" . "https:// ...

  8. installshield 6109错误解决方案

    电脑重装了一下过后,运行打包程序就一直报6109错误,网上也没有查找出相关答案,真是急死了,后来无意发现输出项目的发布路径和当前自己setup的路径不一致,由于移动了文件夹位置,这个路径没有跟随修改, ...

  9. 泛型(Generic)

    当集合中存储的对象类型不同时,那么会导致程序在运行的时候的转型异常 import java.util.ArrayList; import java.util.Iterator; public clas ...

  10. 文件解析库doctotext源码分析

    doctotext中没有make install选项,make后生成可执行文件 在buile目录下面有.so动态库和头文件,需要的可以从这里面拷贝 build/doctotext就是可执行程序.   ...