10.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();
} }
}
}
上面代码把常用的操作列出来了,其他的很多操作。就不一一列举了。。。。。。。。。。。。。
10.C#知识点:操作XML的更多相关文章
- SqlServer知识点-操作xml
一.开发环境 SQL2010 二.开发过程 1.声明一个xml类型变量 DECLARE @xmlInfo XML; SET @xmlInfo = '<CompanyGroup> <C ...
- C#知识点:操作XML
XML是什么就不用说了文本标记语言. 主要纪录如何对XML文件进行增删改查. Xml的操作类都存在System.xml命名空间下面. 应用型的直接上代码 using System; using Sys ...
- 第12章 在.NET中操作XML
12.1 XML概述 12.1.1 为什么要有XML 12.1.2 XML文档结构 (1)文档声明 <?xml version="1.0"encoding="UTF ...
- C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)【转载】
已知有一个XML文件(bookstore.xml)如下: Corets, Eva 5.95 1.插入节点 往节点中插入一个节点: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- VBA中操作XML
OFFICE2007之后使用了OpenXml标准(伟大的改变),定制文本级的Ribbon可以通过修改压缩包内的xml文件来实现. 先学习一下VBA中操作XML的方法 先引用Microsoft XML ...
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...
- 黄聪:C#操作xml SelectNodes,SelectSingleNode通过 xPath 定位class包含Contains的DIV
一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...
- C#操作xml的3种方式
C#操作Xml有很多种方式,这里写出个人常使用的三种方式 XmlDocument DataSet linq to xml 首先声明本次操作使用的xml文件:books.xml:内容如下 <?x ...
- asp.net操作xml(增删查改)
asp.net操作xml 1.xml文档Products.xml <?xml version="1.0" encoding="utf-8"?> &l ...
随机推荐
- WinForm中DataGridView的使用(一) - 基本使用
数据绑定 直接指定源数据(List<T>):this.DataSource = data; 通常也可以直接指定DataTable类型的数据 DataTable dt = new DataT ...
- DevExpress中获取GridControl排序之后的List
public System.Collections.IList GetGridViewFilteredAndSortedData(DevExpress.XtraGrid.Views.Grid.Grid ...
- 【转】C# 之泛型详解
原文地址:https://www.cnblogs.com/yueyue184/p/5032156.html 什么是泛型 我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一 ...
- iis支持asp.net4.0的注册命令使用方法
32位的Windows: 1. 运行->cmd 2. cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 3. aspnet_regiis.exe ...
- DES/3DES/AES区别
公元前400年,古希腊人发明了置换密码.1881年世界上的第一个电话保密专利出现.在第二次世界大战期间,德国军方启用“恩尼格玛”密码机,密码学在战争中起着非常重要的作用. DES 1977年1月,美国 ...
- node - 使用request发送http请求
在nodejs的开发中,有时需要后台去调用其他服务器的接口,这个时候,就需要发送HTTP请求了.有一个简单的工具可以用,Simplified HTTP request client,可以比较方便的模拟 ...
- D14——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D14 20180919内容纲要: 1.html认识 2.常用标签 3.京东html 4.小结 5.练习(简易淘宝html) 1.html初识(HyperText ...
- 【sping揭秘】4、某些无法注册到IOC容器的对象如何交给spring托管
可以实现spring的factoryBean 接口,这样可以加入spring的IOC容器 比如现在有一个类叫MyObject,我们没有这个对象的源码,无法对这个对象进行操作,那么我们如何加入sprin ...
- MetaMask安装使用指南
1.MetaMask(轻钱包) MetaMask是一款在谷歌浏览器Chrome上使用的插件类型的以太坊钱包,该钱包不需要下载,只需要在谷歌浏览器或基于谷歌浏览器内核的其它浏览器(如:360极速浏览器- ...
- How To Scan QRCode For UWP (4)
QR Code的全称是Quick Response Code,中文翻译为快速响应矩阵图码,有关它的简介可以查看维基百科. 我准备使用ZXing.Net来实现扫描二维码的功能,ZXing.Net在Cod ...