说明:可扩展标记语言 eXtensible Markup Language--区分大小写

涉及到的知识点:DOM 文档对象模型

文本文件存储数据缺点:1,不易读取.2,易乱码

1 通过代码创建一个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();
//2创建xml版本描述信息
XmlDeclaration dec= doc.CreateXmlDeclaration("1.0","utf-8",null);
//3将第一行数据添加到文档中
doc.AppendChild(dec);
//4创建根节点
XmlElement books= doc.CreateElement("Books");
//5 将根节点添加到文档对象
doc.AppendChild(books);
//6创建子节点
XmlElement book1 = doc.CreateElement("Book");
//7 将子节点book1添加到根节点下
books.AppendChild(book1); //8 创建book1的子节点--BookName
XmlElement book1Name = doc.CreateElement("BookName");
//9 创建BookName的内容
book1Name.InnerText = "水浒传";
//10 将book1Name添加到Book1节点下
book1.AppendChild(book1Name); //8.1 创建book1的子节点--BookName
XmlElement book1Author = doc.CreateElement("BookAuthor");
//9.1 创建BookAuthor的内容
book1Author.InnerText = "施耐庵";
//10.1 将book1Author添加到Book1节点下
book1.AppendChild(book1Author); //8.2 创建 book1的子节点-BookPrice
XmlElement book1Price = doc.CreateElement("BookPrice");
//9.2 创建BookPrice的内容
book1Price.InnerText = "<xml>100</xml>";
//10.2 将BookPrice添加到book1中
book1.AppendChild(book1Price); //8.3 创建book1的子节点 BookDec
XmlElement book1Dec = doc.CreateElement("BookDec");
//9.3设置bookDec内容
book1Dec.InnerXml="不错<xml></xml>";
//10.3 将bookDec保存到book1中
book1.AppendChild(book1Dec); doc.Save("Book.xml");
Console.WriteLine("保存成功!");
Console.ReadLine();
}
}
}

运行效果

现在可以明白innerText和innerXml的区别了吧:一个队html进行编译,一个不编译

2 创建一个带有属性的xml文档

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml; namespace XMLTest2
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
//1创建版本信息说明
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0","utf-8",null);
//2添加到文档中
doc.AppendChild(dec);
//3 创建根节点-并添加到文档中
XmlElement order = doc.CreateElement("Order");
//3-1将根节点添加到文档中
doc.AppendChild(order);
//4 创建子节点-设置子节点属性-并添加到文档中
XmlElement customerName = doc.CreateElement("CustomerName");
//4-1 设置子节点属性
customerName.InnerXml = "小天狼";
order.AppendChild(customerName); //5添加点OrderNumber
XmlElement orderNumber = doc.CreateElement("OrderNumber");
//5-1设置orderNumber内容
orderNumber.InnerXml = "";
//5-2 添加到order中
order.AppendChild(orderNumber); //6创建items-并添加到order上
XmlElement items = doc.CreateElement("Items");
//6-1添加子节点
XmlElement orderItem1 = doc.CreateElement("OrderItem");
//6-1-1 为OrderItem添加属性
orderItem1.SetAttribute("Name","码表");
orderItem1.SetAttribute("Count","");
items.AppendChild(orderItem1);
//6-2 添加第二个子节点
XmlElement orderItem2 = doc.CreateElement("OrderItem");
orderItem2.SetAttribute("Name","雨衣");
orderItem2.SetAttribute("Count","");
items.AppendChild(orderItem2); //6-2添加到order中
order.AppendChild(items); doc.Save("Order.xml");
Console.WriteLine("保存成功!");
Console.ReadLine();
}
}
}

3通过文档对象模型DOM创建xml

3.1 创建Student类对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test3
{
public class Student
{
public int Age { get; set; }
public string Name { get; set; }
public int ID { get; set; }
public char Gender { get; set; }
}
}

Person

3.2main方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Test3; namespace XMLTest3
{
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>();
list.Add(new Student() { ID = , Name = "yk", Gender = '男', Age = });
list.Add(new Student() { ID = , Name = "xy", Gender = '女', Age = });
list.Add(new Student() { ID = , Name = "下雨了", Gender = '男', Age = });
list.Add(new Student() { ID = , Name = "xtl", Gender = '女', Age = });
list.Add(new Student() { ID = , Name = "逍遥小天狼", Gender = '男', Age = }); XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0","utf-8",null);
doc.AppendChild(dec); //创建根节点
XmlElement person = doc.CreateElement("Person");
doc.AppendChild(person);
//将对象插入到xml中
foreach (Student item in list)
{
XmlElement student = doc.CreateElement("Student");
student.SetAttribute("StudentID",item.ID.ToString());
XmlElement name = doc.CreateElement("Name");
name.InnerXml = item.Name;
student.AppendChild(name);
XmlElement age = doc.CreateElement("Age");
age.InnerXml = item.Age.ToString();
student.AppendChild(age);
XmlElement gender = doc.CreateElement("Gender");
gender.InnerXml = item.Gender.ToString();
student.AppendChild(gender); //将student添加到Person
person.AppendChild(student); } doc.Save("Person.xml");
Console.WriteLine("保存成功");
Console.ReadLine();
}
}
}

运行效果

1

步步为营-20-XML的更多相关文章

  1. SQL SERVER XML 学习总结

    SQL  SERVER  XML  学习总结 最新的项目任务要做一个数据同步的功能,这些天都在做技术准备,主要是用到了微软的Service Broker技术,在熟悉使用该技术的同时,又用到了Sql s ...

  2. XML 与 JSON大PK

    导读 XML 和 JSON 是现今互联网中最常用的两种数据交换格式.XML 格式由 W3C 于 1996 年提出.JSON 格式由 Douglas Crockford 于 2002 年提出.虽然这两种 ...

  3. PHP数组与xml互相转换

    1.数组转xml function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key => $va ...

  4. Bean生命周期及BeanFactory

    1.spring通过BeanFactory灵活配置.管理bean,Spring对管理的bean没有任何特别的要求,完全支持对POJO的管理: 2.BeanFactory有个ApplicationCon ...

  5. OpenCV Haartraining

    opencv_haartraining.exe -data xml -vec pos.vec -bg neg/neg.txt -w 20 -h 20 -mem 144 opencv_haartrain ...

  6. Android的各种Drawable 讲解 大全

    Android把可绘制的对象抽象为Drawable,不同的图形图像资源就代表着不同的drawable类型.Android FrameWork提供了一些具体的Drawable实现,通常在代码中都不会直接 ...

  7. Shape + Selector: Make a Shape as one item of the Selector

    Generally, I use a selector to select pictures or colors to render the normal and the pressed backgr ...

  8. 《转》Spring4 Freemarker框架搭建学习

    这里原帖地址:http://www.cnblogs.com/porcoGT/p/4537064.html 完整配置springmvc4,最终视图选择的是html,非静态文件. 最近自己配置spring ...

  9. 2、android Service 详细用法

    定义一个服务 在项目中定义一个服务,新建一个ServiceTest项目,然后在这个项目中新增一个名为MyService的类,并让它继承自Service,完成后的代码如下所示: ? 1 2 3 4 5 ...

  10. mybatis整合spring 之 基于接口映射的多对一关系

    转载自:http://my.oschina.net/huangcongmin12/blog/83731 mybatis整合spring 之  基于接口映射的多对一关系. 项目用到俩个表,即studen ...

随机推荐

  1. ACPI:Memory错误解决办法

    Linux系统装在vmware12中,打开虚拟机时报错,报错内容大概如下: ACPI:memory_hp:Memory online failed for 0x100000000 - 0x400000 ...

  2. JS 中函数名后面加与不加括号的区别

    a.onmouseover = fn1; a.onmouseout = fn2; function fn1(){ div.className = "erweima show"; } ...

  3. JAVA记录-Spring两大特性

    1.IOC控制反转 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象 ...

  4. Git撤销&回滚操作

    https://blog.csdn.net/ligang2585116/article/details/71094887 开发过程中,你肯定会遇到这样的场景: 场景一: 糟了,我刚把不想要的代码,co ...

  5. 03-Windows Server 2016 IIS的安装与配置

    1. 打开服务器管理器,点击[添加角色和功能选项].        2. 进入“添加角色和功能向导”页面,点击下一步. 3. 安装类型选择[基于角色或基于功能的安装],点击下一步. 4. 进入服务器选 ...

  6. 第二节--Servlet

    1.在tomcat的webapp下新建一个web项目test 要有WEB-INF目录,其下有web.xml.   2.WEB-INF下文件是给tomcat使用的 3.用户访问localhost:848 ...

  7. [C++]Linux之图形界面编程库[curses库]之入门教程

    1. 安装 //方法一 sudo apt-get install libncurses5-dev [ ubuntu 16.04:亲测有效] //方法二 sudo apt-get install ncu ...

  8. eclips环境下开发spring boot项目,application.properties配置文件下中文乱码解决方案

    如以上,application.properties文件下中文乱码.发生乱码一般都是由于编码格式不一样导致的. 打开Window-Preferences-General-content Types-T ...

  9. 用C++的 new 代替 C 的 malloc 进行内存分配

    例子: (int*)malloc(100*sizeof(int)) 是先取得int类型的字节宽度,然后乘100计算后得到400,然后调用malloc,并将400传递给函数,分配400字节的内存空间,但 ...

  10. linux关机时候执行命令脚本或程序

    Write a service file and place it in /etc/systemd/system/beforeshuttingdown.service code: [Unit] Des ...