说明:可扩展标记语言 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. css3让图文不能复制

    -webkit-user-select: none; -ms-user-select: none; -moz-user-select: none; -khtml-user-select: none; ...

  2. SQL Server分页进化

    DataReader.Dataset 数据量太大就用datareader,dataset都读到内存里了,datareader是直接读取数据库. DataReader是一个快速的只进游标 DataRea ...

  3. 阿里云服务器安装SQLServer本地无法远程访问

    新买的阿里云服务器,安装上sqlserver2012,本机连接测试没有问题,但是回到本地,使用ip远程连接报错. 尝试了网上各种办法,都是失败.最后找到原因,原来在阿里云的控制台上有设置: 首先进入安 ...

  4. PHP 将amr音频文件转换为mp3格式

    说下整体思路 1.服务器安装ffmpeg 2.使用ffmpeg -i 指令来转换amr为mp3格式(这个到时候写在PHP代码中,使用exec函数执行即可) 3.在网页端使用HTML5的audio标签来 ...

  5. rest framework错误笔记——AssertionError: Cannot apply DjangoModelPermissionsOrAnonReadOnly on a view that does not set `.queryset` or have a `.get_queryset()` method.

    用到@api_view装饰器时,访问路由查看api数据时,报错: AssertionError: Cannot apply DjangoModelPermissionsOrAnonReadOnly o ...

  6. python - 用类写装饰器

    这里用到了__call__的class内置参数 #类装饰器: class zsq(): #本质是定义一个参数,让装饰的主题传递至__call__方法内部 def __init__(self,obj): ...

  7. freemark简单事例

    工作准备:开发环境myeclipse freemarker.jar(需要下载) 首先引入freemarker.jar包.然后,,,,直接贴代码: 1.创建一个FreemarkerUtil类: pack ...

  8. swift 计算100000以内的 回文数

    ... { var rep = var aa = a repeat{ rep = rep * + aa % aa = aa / }) if(rep == a) { print("\(a)是回 ...

  9. Mask RCNN 简单使用

    涉及到的知识点补充: FasterRCNN:https://www.cnblogs.com/wangyong/p/8513563.html RoIPooling.RoIAlign:https://ww ...

  10. shiroWeb项目-登陆与退出实现(九)

    原理 使用FormAuthenticationFilter过虑器实现 ,原理如下: 将用户没有认证时,请求loginurl进行认证,用户身份和用户密码提交数据到loginurl FormAuthent ...