C# XML入门
2021-06-10 17:02
                         标签:操作   兼容   接下来   dom   入门   出现   one   main   each    什么是XML? XML:可扩展标记语言。 XML的作用: 纯文本,兼容性强。 和HTML的区别: xml: 主要用来处理、存储数据。无规定标签,可扩展。 html:对数据的显示和描述。 语法标签固定。 XML语法特点: 区分大小写。 只能有一个根节点。 标签成对出现。 属性用双引号。 没有预定标签,用什么写什么 文档声明: 注释:  CDATA: 原意文本  xmldocument 操作: 以上方法可以用循环写入。 xdocument 操作。     C# XML入门 标签:操作   兼容   接下来   dom   入门   出现   one   main   each    原文地址:https://www.cnblogs.com/zhangyuhao/p/10578943.html class Program
    {
        static void Main(string[] args)
        {
            //实现xml的写入
            //1、在内存中构建Dom对象
            XmlDocument xmlDoc = new XmlDocument();
            //增加文档说明
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
            xmlDoc.AppendChild(xmlDeclaration);
            //增加根元素
            //  创建根元素
            XmlElement rootElement = xmlDoc.CreateElement("school");
            xmlDoc.AppendChild(rootElement);
            //3、增加子元素,接下来添加的子元素增加到rootElement节点下
            XmlElement xmlClassElement = xmlDoc.CreateElement("class");
            // 为class元素添加id属性
            XmlAttribute attr = xmlDoc.CreateAttribute("id");
            attr.Value = "x01";
            xmlClassElement.Attributes.Append(attr);
            rootElement.AppendChild(xmlClassElement);
            //4、为class创建student节点。
            XmlElement xmlStudentElement = xmlDoc.CreateElement("student");
            // 为student元素添加sid 属性.
            XmlAttribute studentAttr = xmlDoc.CreateAttribute("sid");
            studentAttr.Value = "s011";
            xmlStudentElement.Attributes.Append(studentAttr);
            xmlClassElement.AppendChild(xmlStudentElement);
            //student中增加name节点。
            XmlElement xmlNameElement = xmlDoc.CreateElement("name");
            xmlNameElement.InnerText = "天";
            xmlStudentElement.AppendChild(xmlNameElement);
            //2、将该Dom对象写入xml文件中
            xmlDoc.Save("school.xml");
            Console.WriteLine("ok");
        }
    }
 class Program
    {
        static void Main(string[] args)
        {
            //  通过xdocument 写入文件
            List
 class Program
    {
        static void Main(string[] args)
        {
            //读取XML文件。
            XDocument document = XDocument.Load("List.xml");
            XElement rootElement = document.Root;
            Console.WriteLine("订购人:{0}",rootElement.Element("CustomerName").Value);
            foreach (var item in rootElement.Element("Items").Elements("OrderItem"))
            {
                Console.WriteLine("商品名称:{0}",item.Attribute("Name").Value);
            }
            
        }  
    }