C#中XML基础用法有哪些

发布时间:2021-12-18 12:58:50 作者:小新
来源:亿速云 阅读:131

这篇文章主要介绍C#中XML基础用法有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

什么是XML?

XML:可扩展标记语言。

XML的作用:

纯文本,兼容性强。

和HTML的区别:

xml: 主要用来处理、存储数据。无规定标签,可扩展。

html:对数据的显示和描述。 语法标签固定。

XML语法特点:

区分大小写。

只能有一个根节点。

标签成对出现。

属性用双引号。

没有预定标签,用什么写什么

文档声明:<?xml version=".." encoding="...">

注释: <!--   -->

CDATA: 原意文本 <![CDATA[..] ] >

xmldocument 操作:

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");
        }
    }

以上方法可以用循环写入。

xdocument 操作。

class Program
    {
        static void Main(string[] args)
        {
            //  通过xdocument 写入文件
            List<Person> list = new List<Person>();
            list.Add(new Person() { Name = "Sam", Age = 18 });
            list.Add(new Person() { Name = "Penny", Age = 20 });
            // 1、 创建Dom对象。
            XDocument xDoc = new XDocument();
            XDeclaration xDec = new XDeclaration("1.0", "utf-8", null);
            // 设置文档定义
            xDoc.Declaration = xDec;
            //2、创建根节点
            XElement rootElement = new XElement("List");
            xDoc.Add(rootElement);
            //3、循环创建节点
            for (int i = 0; i < list.Count; i++)
            {
                XElement PersonElement = new XElement("Person");
                PersonElement.SetAttributeValue("id", (i + 1).ToString());

                PersonElement.SetElementValue("Name", list[i].Name);
                PersonElement.SetElementValue("Age", list[i].Age);
                rootElement.Add(PersonElement);
            }
            xDoc.Save("List.xml");
            Console.WriteLine("ok");
        }
    }
    class Person
    {
        public string Name { get; set; }
        public  int Age { get; set; }
    }
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);
            }
            
        }  
    }

以上是“C#中XML基础用法有哪些”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. C#中“?”有三种用法
  2. xml基础

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

xml

上一篇:JSP动态如何实现web网页登陆和注册功能

下一篇:如何进行springboot配置templates直接访问的实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》