步步为营-20-XML
生活随笔
收集整理的這篇文章主要介紹了
步步为营-20-XML
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
說明:可擴展標記語言?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的子節點--BookNameXmlElement book1Name = doc.CreateElement("BookName");//9 創建BookName的內容book1Name.InnerText = "水滸傳";//10 將book1Name添加到Book1節點下 book1.AppendChild(book1Name);//8.1 創建book1的子節點--BookNameXmlElement book1Author = doc.CreateElement("BookAuthor");//9.1 創建BookAuthor的內容book1Author.InnerText = "施耐庵";//10.1 將book1Author添加到Book1節點下 book1.AppendChild(book1Author);//8.2 創建 book1的子節點-BookPriceXmlElement book1Price = doc.CreateElement("BookPrice");//9.2 創建BookPrice的內容book1Price.InnerText = "<xml>100</xml>";//10.2 將BookPrice添加到book1中 book1.AppendChild(book1Price);//8.3 創建book1的子節點 BookDecXmlElement 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();}} } 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添加點OrderNumberXmlElement 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; }} } Person3.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 = 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
?
轉載于:https://www.cnblogs.com/YK2012/p/6722617.html
總結
以上是生活随笔為你收集整理的步步为营-20-XML的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apriori算法介绍(Python实现
- 下一篇: 添加Mysql普通用户来管理hive