二、操作XML DOM:XML Document
需要添加的命名空间:
using System.Xml;
一、创建xml文件:
1、XmlDocument方式创建
XmlDocument xmldoc = new XmlDocument();
//加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
XmlDeclaration xmldecl;
xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmldoc.AppendChild(xmldecl);
//加入一个根元素
XmlElement xmlelem = xmldoc.CreateElement("", "Employees", "");
xmldoc.AppendChild(xmlelem);
//加入另外一个元素
; i < ; i++)
{
XmlNode root = xmldoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个<Node>节点
xe1.SetAttribute("genre", "李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性
XmlElement xesub1 = xmldoc.CreateElement("title");
xesub1.InnerText = "CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<Node>节点中
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);//添加到<Employees>节点中
}
//保存创建好的XML文档
xmldoc.Save("../../data.xml");
结果:生成了名为data.xml的文件,内容如下,
<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
</Employees>
2、XmlTextWriter方式创建
string strFilename = "../../data1.xml";
XmlTextWriter xmlWriter = new XmlTextWriter(strFilename, Encoding.Default);//创建一个xml文档
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Employees");
xmlWriter.WriteStartElement("Node");
xmlWriter.WriteAttributeString("genre", "李赞红");
xmlWriter.WriteAttributeString("ISBN", "2-3631-4");
xmlWriter.WriteStartElement("title");
xmlWriter.WriteString("CS从入门到精通");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("author");
xmlWriter.WriteString("候捷");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("price");
xmlWriter.WriteString("58.3");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.Close();
结果:
<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
</Employees>
3、加载已有的XML文件:XmlDocument.Load()
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("data.xml"));
XmlElement root = xmldoc.DocumentElement;//获取根元素
foreach (XmlNode node in root.ChildNodes)
{
if (node.Attributes["gere"].Value == "a")
{
foreach (XmlNode node1 in node.ChildNodes)
{
if (node1.Name == "author")
{
node1.InnerText = "aa";//修改文本
}
}
}
}
4、创建XML片段:XmlDocument.CreateDocumentFragment()
XmlDocument xmldoc = new XmlDocument(); XmlDocumentFragment docFrag = xmldoc.CreateDocumentFragment(); docFrag.InnerXml = "<item>aa</item>"; Console.Write(docFrag.InnerXml); XmlElement root = xmldoc.DocumentElement; root.AppendChild(docFrag);
二、添加一个结点:AppendChild
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("../../data.xml");
XmlNode root = xmlDoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1 = xmlDoc.CreateElement("Node");//创建一个<Node>节点
xe1.SetAttribute("genre", "张三");//设置该节点genre属性
xe1.SetAttribute("ISBN", "1-1111-1");//设置该节点ISBN属性
XmlElement xesub1 = xmlDoc.CreateElement("title");
xesub1.InnerText = "C#入门帮助";//设置文本节点
xe1.AppendChild(xesub1);//添加到<Node>节点中
XmlElement xesub2 = xmlDoc.CreateElement("author");
xesub2.InnerText = "高手";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("price");
xesub3.InnerText = "158.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<Employees>节点中
xmlDoc.Save("../../data.xml");
结果:在xml原有的内容里添加了一个结点,内容如下,
<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>高手</author>
<price>158.3</price>
</Node>
</Employees>
三、修改结点的值(属性和子结点):
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("../../data.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.GetAttribute("genre") == "张三")//如果genre属性值为“张三”
{
xe.SetAttribute("genre", "update张三");//则修改该属性为“update张三”
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == "author")//如果找到
{
xe2.InnerText = "亚胜";//则修改
}
}
}
}
xmlDoc.Save("../../data.xml");//保存。
结果:将原来的所有结点的信息都修改了,xml的内容如下,
<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="update张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>亚胜</author>
<price>158.3</price>
</Node>
</Employees>
四、修改结点(添加结点的属性和添加结点的自结点)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("../../data.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute(");//添加属性
XmlElement xesub = xmlDoc.CreateElement("flag");
xesub.InnerText = ";
xe.AppendChild(xesub);//添加子节点
}
xmlDoc.Save("../../data.xml");
结果:每个结点的属性都添加了一个,子结点也添加了一个,内容如下,
<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
<flag>1</flag>
</Node>
<Node genre="李赞红" ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
<flag>1</flag>
</Node>
<Node genre="update张三" ISBN="1-1111-1" test="111111">
<title>C#入门帮助</title>
<author>亚胜</author>
<price>158.3</price>
<flag>1</flag>
</Node>
</Employees>
五、删除结点中的某一个属性:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("../../data.xml");
XmlNodeList xnl = xmlDoc.SelectSingleNode("Employees").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
xe.RemoveAttribute("genre");//删除genre属性
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == "flag")//如果找到
{
xe.RemoveChild(xe2);//则删除
}
}
}
xmlDoc.Save("../../data.xml");
结果:删除了结点的一个属性和结点的一个子结点,内容如下,
<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node ISBN="1-1111-1" test="111111">
<title>C#入门帮助</title>
<author>亚胜</author>
<price>158.3</price>
</Node>
</Employees>
六、删除结点:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("../../data.xml");
XmlNode root = xmlDoc.SelectSingleNode("Employees");
XmlNodeList xnl = xmlDoc.SelectSingleNode("Employees").ChildNodes;
; i < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe.GetAttribute("genre") == "张三")
{
root.RemoveChild(xe);
;
}
}
xmlDoc.Save("../../data.xml");
结果:删除了符合条件的所有结点,原来的内容:
<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>高手</author>
<price>158.3</price>
</Node>
<Node genre="张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>高手</author>
<price>158.3</price>
</Node>
</Employees>
删除后的内容:
<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
</Employees>
七、替换子元素
XmlElement elem=xmldoc.CreateElement("title");
elem.InnerText="XX";
root.ReplaceChild(elem,root.FirstChild);
八、按照文本文件读取xml:StreamReader
StreamReader myFile = new StreamReader("../../data.xml", System.Text.Encoding.Default);
//注意System.Text.Encoding.Default
string myString = myFile.ReadToEnd();//myString是读出的字符串
myFile.Close();
Console.Write(myString);
二、操作XML DOM:XML Document的更多相关文章
- XML DOM(Document Object Model)
1.XML DOM 是用于获取.更改.添加或删除 XML 元素的标准.2.节点(XML 文档中的每个成分都是一个节点): 整个文档是一个文档节点: 每个XML元素是一个元素 ...
- xml dom minidom
一. xml相关术语: 1.Document(文档): 对应一个xml文件 2.Declaration(声明): <?xml version="1.0" encoding=& ...
- 雷林鹏分享:XML DOM
XML DOM DOM(Document Object Model 文档对象模型)定义了访问和操作文档的标准方法. XML DOM XML DOM(XML Document Object Model) ...
- XML DOM 知识点
第一部分[DOM基础] DOM介绍: 1.什么是 HTML DOM? HTML DOM 定义了所有 HTML 元素的对象和属性,以及访问它们的方法(接口). 2.什么是 XML DOM? XML DO ...
- Commons JXPath - DOM/JDOM Document Access
除了 JavaBean,JXPath 也可以访问 DOM/JDOM. 示例 XML: <?xml version="1.0" encoding="utf-8&quo ...
- XML DOM (Document Object Model) 定义了访问和操作 XML 文档的标准方法。
XML DOM DOM 把 XML 文档视为一种树结构.通过这个 DOM 树,可以访问所有的元素.可以修改它们的内容(文本以及属性),而且可以创建新的元素.元素,以及它们的文本和属性,均被视为节点. ...
- Java SE之XML<二>XML DOM与SAX解析
[文档整理系列] Java SE之XML<二>XML DOM与SAX解析 XML编程:CRUD(Create Read Update Delete) XML解析的两种常见方式: DOM(D ...
- 【二十八】xml编程(dom\xpath\simplexml)
1.xml基础概念 作用范围: 作为程序通讯的标准. 作为配置文件. 作为小型数据库. xml语法: <根标签> <标签 元素="元素值" ...>< ...
- xml ---DOM操作
package day03.xml; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; ...
- PHP使用DOM XML操作XML[总结]
1.前言 XML树状层次结构鲜明,非常适合作为配置文件.PHP中可以使用DOM XML来操作XML.本文总结一下PHP使用DOM XML创建.添加节点.查询XML文件. 2.使用DOM XML XML ...
随机推荐
- MySQL面试 - 读写分离
MySQL面试 - 读写分离 面试题 你们有没有做 MySQL 读写分离?如何实现 MySQL 的读写分离?MySQL 主从复制原理的是啥?如何解决 MySQL 主从同步的延时问题? 面试官心理分析 ...
- NetworkX一个图论与复杂网络建模工具
NetworkX是一个图论与复杂网络建模工具,采用Python语言开发,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网络数据分析.仿真建模等工作.(1)NetworkX支持创建简单无向图.有向 ...
- python3快速安装升级pip3
一.下载地址: 获取get-pip.py安装文件: 官网链接:https://bootstrap.pypa.io/get-pip.py 百度云链接:https://pan.baidu.com/s/14 ...
- AJAX一些注释掉的语句
var sysdept=JSON.parse(localStorage.getItem("loginSysUser")); for(var o in sysdept){ alert ...
- SRID (空间引用识别号, 坐标系)【转】
SRID (空间引用识别号, 坐标系)-云栖社区-阿里云 Spatial Reference List -- Spatial Reference Chapter 8. PostGIS Referenc ...
- S04_CH03_QSPI烧写LINUX系统
S04_CH03_QSPI烧写LINUX系统 3.1概述 3.2搭建硬件系统 本章硬件工程还是使用<S04_CH01_搭建工程移植LINUX/测试EMMC/VGA>所搭建的VIVADO工程 ...
- Linux 内核错误码
#define EPERM 1 /* Operation not permitted */#define ENOENT 2 /* No such ...
- Linux下 sftp服务配置
查看openssh的版本,使用ssh -V 命令来查看openssh的版本,版本必须大于4.8p1,低于的这个版本需要升级. 参考博客:https://yq.aliyun.com/articles/6 ...
- 网页修改<title ></title >标签内容
document.title = 'xxxxxx';
- 深度学习的激活函数 :sigmoid、tanh、ReLU 、Leaky Relu、RReLU、softsign 、softplus、GELU
深度学习的激活函数 :sigmoid.tanh.ReLU .Leaky Relu.RReLU.softsign .softplus.GELU 2019-05-06 17:56:43 wamg潇潇 阅 ...