柚子快报邀请码778899分享:步步为营-20-XML

http://yzkb.51969.com/

说明:可扩展标记语言 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 = "100";

//10.2 将BookPrice添加到book1中

book1.AppendChild(book1Price);

//8.3 创建book1的子节点 BookDec

XmlElement book1Dec = doc.CreateElement("BookDec");

//9.3设置bookDec内容

book1Dec.InnerXml="不错";

//10.3 将bookDec保存到book1中

book1.AppendChild(book1Dec);

doc.Save("Book.xml");

Console.WriteLine("保存成功!");

Console.ReadLine();

}

}

}

View Code

运行效果

现在可以明白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 = "10000";

//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","10001");

items.AppendChild(orderItem1);

//6-2 添加第二个子节点

XmlElement orderItem2 = doc.CreateElement("OrderItem");

orderItem2.SetAttribute("Name","雨衣");

orderItem2.SetAttribute("Count","100");

items.AppendChild(orderItem2);

//6-2添加到order中

order.AppendChild(items);

doc.Save("Order.xml");

Console.WriteLine("保存成功!");

Console.ReadLine();

}

}

}

View Code

 

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 list = new List();

list.Add(new Student() { ID = 1, Name = "yk", Gender = '男', Age = 16 });

list.Add(new Student() { ID = 2, Name = "xy", Gender = '女', Age = 16 });

list.Add(new Student() { ID = 3, Name = "下雨了", Gender = '男', Age = 20 });

list.Add(new Student() { ID = 4, Name = "xtl", Gender = '女', Age = 16 });

list.Add(new Student() { ID = 5, Name = "逍遥小天狼", Gender = '男', Age = 30 });

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();

}

}

}

View Code

运行效果

1

 

柚子快报邀请码778899分享:步步为营-20-XML

http://yzkb.51969.com/

好文阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。