一、概念及特征:

1. XML 指可扩展标记语言(Extensible Markup Language),用户可以自己定义标签。XML 被设计用来传输和存储数据,而 HTML 用于格式化并显示数据,并且HTML不能自定义标签。

2. XML 文档形成一种树结构, XML 文档必须包含根元素。该元素是所有其他元素的父元素。XML 文档中的元素形成了一棵文档树。这棵树从根部开始,并扩展到树的最底端。

3. XML中所有元素都必须有关闭标签, XML 必须正确地嵌套, XML 的属性值须加引号, XML 标签对大小写敏感。

二、基本格式示例:

<bookstore>

 <book category="CHILDREN">

  <title>Harry Potter</title>

  <author>J K. Rowling</author>

  <year>2005</year>

  <price>29.99</price>

 </book>

 </bookstore>

三、使用XMLDocument读写XML

protected void btnCreate_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("bookstore.xml"));
XmlNode root = xmlDoc.SelectSingleNode("bookstore");
XmlElement xe1 = xmlDoc.CreateElement("book");
xe1.SetAttribute("genre", "李赞红");
xe1.SetAttribute("ISBN", "2-3631-4");
XmlElement xesub1 = xmlDoc.CreateElement("title");
xesub1.InnerText = "CS从入门到精通";
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("author");
xesub2.InnerText = "候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("price");
xesub3.InnerText = "58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);
xmlDoc.Save(Server.MapPath("bookstore.xml"));
} protected void EditNodes_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("bookstore.xml"));
XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == "李赞红")
{
xe.SetAttribute("genre", "update李赞红");
XmlNodeList nls = xe.ChildNodes;
foreach (XmlNode xn1 in nls)
{
XmlElement xe2 = (XmlElement)xn1;
if (xe2.Name == "author")
{
xe2.InnerText = "亚胜";
break;
}
}
break;
}
}
xmlDoc.Save(Server.MapPath("bookstore.xml"));
} protected void btnDelete_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("bookstore.xml"));
XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == "fantasy")
{
xe.RemoveAttribute("genre");
}
else if (xe.GetAttribute("genre") == "update李赞红")
{
xe.RemoveAll();
}
}
xmlDoc.Save(Server.MapPath("bookstore.xml"));
}

四、使用LINQ to XML读写XML,LINQ to XML 最重要的优势是它与 Language-Integrated Query (LINQ) 的集成。

private void CreateXmlFile()
{ ///设置新的XML文件保存的地址
string xmlFilePath = Server.MapPath("Books.xml");
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "no"),
new XElement("Books",
new XElement("Book",
new XAttribute("ID", "104"), ///添加属性ID
new XElement("No", "0004"), ///添加元素No
new XElement("Name", "Book 0004"), ///添加元素Name
new XElement("Price", "300"), ///添加元素Price
new XElement("Remark", "This is a book 0004.") ///添加元素Remark
)
)
);
///保存为XML文件
doc.Save(xmlFilePath);
///显示XML文件的内容
Response.Write(doc);
///设置网页显示的形式为XML文件
Response.ContentType = "text/xml";
Response.End();
} private void AddXmlElement()
{ ///导入XML文件
string xmlFilePath = Server.MapPath("Books.xml");
XElement xe = XElement.Load(xmlFilePath);
///创建一个新的节点
XElement book = new XElement("Book",
new XAttribute("ID", "105"), ///添加属性ID
new XElement("No", "0005"), ///添加元素No
new XElement("Name", "Book 0005"), ///添加元素Name
new XElement("Price", "500"), ///添加元素Price
new XElement("Remark", "This is a book 0005.") ///添加元素Remark
);
///添加节点到文件中,并保存
xe.Add(book);
xe.Save(xmlFilePath);
///显示XML文件的内容
Response.Write(xe);
///设置网页显示的形式为XML文件
Response.ContentType = "text/xml";
Response.End();
} private void UpdateXmlElement()
{///导入XML文件
string xmlFilePath = Server.MapPath("Books.xml");
XElement xe = XElement.Load(xmlFilePath);
///查找被替换的元素
IEnumerable<XElement> element = from e in xe.Elements("Book")
where e.Attribute("ID").Value == "104"
select e;
///替换为新元素,并保存
if (element.Count() > 0)
{
XElement first = element.First();
///设置新的属性
first.SetAttributeValue("ID", "106");
///替换新的节点
first.ReplaceNodes(
new XElement("No", "0006"), ///添加元素No
new XElement("Name", "Book 0006"), ///添加元素Name
new XElement("Price", "600"), ///添加元素Price
new XElement("Remark", "This is a book 0006.") ///添加元素Remark
);
}
xe.Save(xmlFilePath);
///显示XML文件的内容
Response.Write(xe);
///设置网页显示的形式为XML文件
Response.ContentType = "text/xml";
Response.End();
} private void RemoveXmlElement()
{
///导入XML文件
string xmlFilePath = Server.MapPath("Books.xml");
XElement xe = XElement.Load(xmlFilePath);
///查找被删除的元素
IEnumerable<XElement> element = from e in xe.Elements()
where (string)e.Element("Name") == "Book 0006"
select e;
///删除指定的元素,并保存
if (element.Count() > 0) { element.First().Remove(); }
xe.Save(xmlFilePath);
///显示XML文件的内容
Response.Write(xe);
///设置网页显示的形式为XML文件
Response.ContentType = "text/xml";
Response.End();
}

XML基本概念及增删改查操作的更多相关文章

  1. 用dom4j解析xml文件并执行增删改查操作

    转自:https://www.aliyun.com/jiaocheng/1339446.html xml文件: <?xml version="1.0" encoding=&q ...

  2. MySQL基本概念及增删改查操作

    SQL.DB.DBMS关系 DB: DataBase(数据库,数据库实际上在硬盘上以文件的形式存在) DBMS: DataBase Management System(数据库管理系统,常见的有:MyS ...

  3. 如何搭建一个WEB服务器项目(二)—— 对数据库表进行基本的增删改查操作

    使用HibernateTemplate进行增删改查操作 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出 ...

  4. python 全栈开发,Day124(MongoDB初识,增删改查操作,数据类型,$关键字以及$修改器,"$"的奇妙用法,Array Object 的特殊操作,选取跳过排序,客户端操作)

    一.MongoDB初识 什么是MongoDB MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介 ...

  5. MyBatis批量增删改查操作

      前文我们介绍了MyBatis基本的增删该查操作,本文介绍批量的增删改查操作.前文地址:http://blog.csdn.net/mahoking/article/details/43673741 ...

  6. Elasticsearch学习系列之单模式下API的增删改查操作

    这里我们通过Elasticsearch的marvel插件实现单模式下API的增删改查操作 索引的初始化操作 创建索引之前可以对索引进行初始化操作,比如先指定shard数量以及replicas的数量 代 ...

  7. Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作

    Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: ​ ​ ​  ​ ​ 由于如果只使用一张表存储所有的数据,就会操作数 ...

  8. MyBatis学习之简单增删改查操作、MyBatis存储过程、MyBatis分页、MyBatis一对一、MyBatis一对多

    一.用到的实体类如下: Student.java package com.company.entity; import java.io.Serializable; import java.util.D ...

  9. 学习MyBatis必知必会(5)~了解myBatis的作用域和生命周期并抽取工具类MyBatisUtil、mybatis执行增删改查操作

    一.了解myBatis的作用域和生命周期[错误的使用会导致非常严重的并发问题] (1)SqlSessionFactoryBuilder [ 作用:仅仅是用来创建SqlSessionFactory,作用 ...

随机推荐

  1. 1Q84 BOOK 1-3 笔记

    1Q84 BOOK 1-3 村上春树 18个笔记     ◆ 第2章 青豆 不要被外表迷惑   >> 在这三十年的人生中,究竟听过多少次同样的台词?为了这个姓氏,曾经多少次遭人调侃?如果不 ...

  2. Python里的一些注释规范

    写代码注释是一件很重要的事情,如果你写的一段函数给别人调用那么往往都需要配上一些基本的注释.写好代码可以让别人容易阅读你的代码.试想一 下:如果你在github上面找到一段你想要的代码,这段代码有20 ...

  3. 「HAOI2015」「LuoguP3178」树上操作(树链剖分

    题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都增 ...

  4. iOS多线程GCD的简单使用

    在iOS开发中,苹果提供了三种多线程技术,分别是: (1)NSThread (2)NSOperation (3)GCD 简单介绍一下GCD的使用. GCD全称 Grand Central Dispat ...

  5. 最优配餐_暴力bfs

    问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务.随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问题. 栋栋的连锁店所在的区域可以看成是一个n×n的方格图(如下图所示),方格的格点上的 ...

  6. A. Meeting of Old Friends

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  7. 洛谷 - P1162 - 填涂颜色 - 简单搜索

    https://www.luogu.org/problemnew/show/P1162 在外面加一圈0把0连起来,然后把所有0换成2,再从(0,0)把连通的2全部改回来. 这也是一个判断内外圈的好办法 ...

  8. 死磕了一题模拟 A掉的时候我是爽的

    ZOJ 3490 没有题解. 一句话,要么贴代码,要么自己打. #include<cstdio> #include<iostream> #include<vector&g ...

  9. 每天一水poj1502【最短路】

    #include<iostream> #include<cstdio> #include<string.h> #include<algorithm> u ...

  10. AtCoder Grand Contest 005 C - Tree Restoring

    题目传送门:https://agc005.contest.atcoder.jp/tasks/agc005_c 题目大意: 给定一个长度为\(N\)的整数序列\(A_i\),问能否构造一个\(N\)个节 ...