XML

时间:2019-04-28 00:40:27   收藏:0   阅读:168

XML可扩展的标记语言,存储数据,轻型数据库;HTML显示数据

注意:严格区分大小写,标签成对出现,有且只有有一个根节点

节点:标签就是节点

元素:所有的内容都是元素,元素包含节点

1、创建XML文档

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace XML
{
  class Program
  {
    static void Main(string[] args)
  {
//通过代码创建XML文档
//1、引用命名空间
//2、创建XML文档对象
    XmlDocument doc = new XmlDocument();
//3、创建第一行描述信息,并且添加到doc文档中
    XmlDeclaration dec = doc.CreateXmlDeclaration("1.0","utf-8",null);
    doc.AppendChild(dec);
//4、创建根节点
    XmlElement books = doc.CreateElement("Books");
//将根节点添加到文本中
    doc.AppendChild(books);
//5、给根节点Books创建子节点
    XmlElement book1 = doc.CreateElement("Book");
//将Book添加到根节点
    books.AppendChild(book1);
//6、给book1添加子节点
    XmlElement name1 = doc.CreateElement("Name");
    name1.InnerText = "西游记";
    book1.AppendChild(name1);

    XmlElement price1 = doc.CreateElement("Price");
    price1.InnerText = "10";
    book1.AppendChild(price1);

    XmlElement des1 = doc.CreateElement("Des");
    des1.InnerText = "西天取经";
    book1.AppendChild(des1);

    XmlElement book2 = doc.CreateElement("Book");
    books.AppendChild(book2);

    XmlElement name2 = doc.CreateElement("Name");
    name2.InnerText = "水浒传";
    book2.AppendChild(name2);

    XmlElement price2 = doc.CreateElement("Price");
    price2.InnerText = "80";
    book2.AppendChild(price2);

    XmlElement des2 = doc.CreateElement("Des");
    des2.InnerText = "英雄";
    book2.AppendChild(des2);

    doc.Save("Books.xml");
    Console.WriteLine("保存成功");
    Console.ReadKey();

    }
  }
}

2、创建带属性的XML文档

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace XML属性
{
  class Program
  {
    static void Main(string[] args)
  {
    XmlDocument doc = new XmlDocument();
    XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
    doc.AppendChild(dec);

    XmlElement order = doc.CreateElement("Order");
    doc.AppendChild(order);

    XmlElement customerName = doc.CreateElement("CustomerName");
    customerName.InnerText = "你好";
    order.AppendChild(customerName);

    XmlElement customerNumber = doc.CreateElement("customerNumber");
    customerNumber.InnerText = "1000001";
    order.AppendChild(customerNumber);

    XmlElement items = doc.CreateElement("Items");
    order.AppendChild(items);

    XmlElement orderItem1 = doc.CreateElement("orderItem");
//给节点添加属性
    orderItem1.SetAttribute("Name", "码表");
    orderItem1.SetAttribute("Count", "10");
    items.AppendChild(orderItem1);


    XmlElement orderItem2 = doc.CreateElement("orderItem");
//给节点添加属性

    orderItem2.SetAttribute("Name", "码表");
    orderItem2.SetAttribute("Count", "10");
    items.AppendChild(orderItem2);

    XmlElement orderItem3 = doc.CreateElement("orderItem");
//给节点添加属性
    orderItem3.SetAttribute("Name", "码表");
    orderItem3.SetAttribute("Count", "10");
    items.AppendChild(orderItem3);

    doc.Save("Order.xml");
    Console.WriteLine("保存成功");
    Console.ReadKey();

}
}
}

 

原文:https://www.cnblogs.com/lz-huihui/p/10781086.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!