C#知识点:操作XML
XML是什么就不用说了文本标记语言。
主要纪录如何对XML文件进行增删改查。
Xml的操作类都存在System.xml命名空间下面。
应用型的直接上代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml; namespace XMLTest
{
class Program
{
static void Main(string[] args)
{
//1.创建XML文档对象
XmlDocument doc = new XmlDocument(); //创建头
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null); //添加节点
doc.AppendChild(xmlDeclaration);
XmlElement xmlElement = doc.CreateElement("Persons"); //给节点添加属性
xmlElement.SetAttribute("Name", "一小时小超人");
doc.AppendChild(xmlElement); XmlElement xmlElement1 = doc.CreateElement("Person");
//给节点添加文字
xmlElement1.InnerXml = "小超人";
xmlElement.AppendChild(xmlElement1);
doc.Save("Test.xml"); }
}
}

<?xml version="1.0" encoding="UTF-8"?>
<Persons Name="一小时小超人">
<Person>小超人</Person>
</Persons>
这个地方主要讲一下 XmlElement.InnerXml和XmlElement.InnerText的区别。代码演示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml; namespace XMLTest
{
class Program
{
static void Main(string[] args)
{
//1.创建XML文档对象
XmlDocument doc = new XmlDocument(); //创建头
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null); //添加节点
doc.AppendChild(xmlDeclaration);
XmlElement xmlElement = doc.CreateElement("Persons"); //给节点添加属性
xmlElement.SetAttribute("Name", "一小时小超人"); doc.AppendChild(xmlElement); XmlElement xmlElement1 = doc.CreateElement("Person");
//给节点添加文字
xmlElement1.InnerXml = "<演示>小超人</演示>";
xmlElement.AppendChild(xmlElement1);
XmlElement xmlElement2 = doc.CreateElement("Person");
//给节点添加文字
xmlElement2.InnerText = "<演示>小超人</演示>";
//给节点添加属性
xmlElement2.SetAttribute("name", "一小时小超人");
xmlElement.AppendChild(xmlElement2);
doc.Save("Test.xml");
}
}
}



<?xml version="1.0" encoding="UTF-8"?>
<Persons Name="一小时小超人">
<Person>
<演示>小超人</演示>
</Person>
<Person name="一小时小超人"><演示>小超人</演示></Person>
</Persons>


很明显的看出来如果字符串是个标签,Interxml会当成标签给你添加,innterText会转义。
下面演示一下读取操作

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml; namespace XMLTest
{
class Program
{
static void Main(string[] args)
{
//1.创建XML文档对象
XmlDocument doc = new XmlDocument();
if (File.Exists("Test.xml"))
{
//通过文件名加载Xml,也可以通过流之类的,其他重载方法,看文档。
doc.Load("Test.xml"); //获取根节点
XmlElement xmlElement = doc.DocumentElement; //获取根节点下面的子节点集合
XmlNodeList nodeList = xmlElement.ChildNodes;
//循环取每一个子节点
foreach (XmlNode item in nodeList)
{ Console.WriteLine(item.Name); //获取节点属性
//string attributesValue=item.Attributes["属性名称"].Value;
}
Console.ReadKey();
} }
}
}
C#知识点:操作XML的更多相关文章
- 10.C#知识点:操作XML
知识点目录==========>传送门 XML是什么就不用说了文本标记语言. 主要纪录如何对XML文件进行增删改查. Xml的操作类都存在System.xml命名空间下面. 应用型的直接上代码 ...
- SqlServer知识点-操作xml
一.开发环境 SQL2010 二.开发过程 1.声明一个xml类型变量 DECLARE @xmlInfo XML; SET @xmlInfo = '<CompanyGroup> <C ...
- VBA中操作XML
OFFICE2007之后使用了OpenXml标准(伟大的改变),定制文本级的Ribbon可以通过修改压缩包内的xml文件来实现. 先学习一下VBA中操作XML的方法 先引用Microsoft XML ...
- Asp.Net 操作XML文件的增删改查 利用GridView
不废话,直接上如何利用Asp.NET操作XML文件,并对其属性进行修改,刚开始的时候,是打算使用JS来控制生成XML文件的,但是最后却是无法创建文件,读取文件则没有使用了 index.aspx 文件 ...
- php中通过DOM操作XML
DOM文档在js里早就接触过,知道DOM不但可以操作html文档,还可以操作XHTML,XML等文档,有着极强的通用性,下面我们通过两个小例子,看看在PHP中是如何用DOM操作XML文档的,和js中差 ...
- 使用dom4j操作XML
DOM4J介绍 DOM4J是使用Java语言编写的,用于读写及操作XML的一套组件,DOM4J同时具有DOM修改文件的优点和SAX读取快速的优点. DOM4J的使用 首先下载dom4j的JAR包,我用 ...
- 使用JDOM操作XML
JDOM介绍 JDOM是使用Java语言编写的,用于读写及操作XML的一套组件,Jdom同时具有DOM修改文件的优点和SAX读取快速的优点. JDOM的使用 首先下载JDOM的JAR包,本文使用的是j ...
- php : DOM 操作 XML
DOM 操作 XML 基本用法 XML文件: person.XML <?xml version="1.0" encoding="utf-8" ?> ...
- Strus2第一次课:dom4j操作xml
先从底层的xml操作技术记录: 当我们新建一个项目,什么架包都没加入的时候,java提供了 org.w3c.dom给我们操作xml里面的元素 import org.w3c.dom.Document; ...
随机推荐
- 这几个冷门却实用的 Python 库,我爱了!
- @RequestMapping 参数详解
引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为applicatio ...
- 聊聊WindowServer那些事!
前言说明 使用工具:VS2019 思考为什么要使用WindowServer,它能做什么了?(后面解答) 一:什么是WindowServer?(我们做的是一个什么东西?) Microso ...
- http接口封装mqtt协议
前言 .Net Core 3.1 WebApi 列出了mqtt客户端的封装目的是为了了解运作机制 1.封装mqtt客户端 mqtt底层协议基于MQTTnet 版本2.8.5 github地址 实例化[ ...
- Bytom 储蓄分红 DAPP 开发指南
储蓄分红DAPP 储蓄分红合约简介 储蓄分红合约指的是项目方发起了一个锁仓计划(即储蓄合约和取现合约),用户可以在准备期自由选择锁仓金额参与该计划,等到锁仓到期之后还可以自动获取锁仓的利润.用户可以在 ...
- C#算法设计排序篇之07-希尔排序(附带动画演示程序)
希尔排序(Shell's Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/687 访问. 希尔排序是插入排序的 ...
- 使用Tensorflow搭建自编码器(Autoencoder)
自编码器是一种数据压缩算法,其中数据的压缩和解压缩函数是数据相关的.从样本中训练而来的.大部分自编码器中,压缩和解压缩的函数是通过神经网络实现的. 1. 使用卷积神经网络搭建自编码器 导入MNIST数 ...
- java实现一个简单的爬虫小程序
前言 前些天无意间在百度搜索了一下以前写过的博客 我啥时候在这么多不知名的网站上发表博客了???点进去一看, 内容一模一样,作者却不是我... 然后又去搜了其他篇博客,果然,基本上每篇都在别的网站上有 ...
- 串口线接Linux设备U盘安装系统和直接安装设备接显示屏2种方式不同
Firmware Bug]: TSC_DEADLINE disabled due to Errata; please update microcode to version: 0x22 (or lat ...
- Cirros镜像
Openstack的开发,基本都使用这个image来测试,因为他比较小,只有10M. 镜像介绍 镜像的地址: https://launchpad.net/cirros/trunk/0.3.0/+dow ...