XML学习

来源:互联网 发布:手机端怎么找淘宝客服 编辑:程序博客网 时间:2024/06/09 22:59
XMLExtansible markup Language,可扩展标记语言)是一种以简单文本格式的方式存储数据,可以被任何计算机读取。

一、XML文档
1、XML元素

XML元素包含一个开始标记(start tag)、元素中的数据和结束标记(end tag)。开始和结束标签也被称为开放标签和闭合标签。
<book>APUE<\bool>
元素中数据可以为空:
<book></bool>
简单语法为:
<book/>
与HTML相比,XML没有任何预定义元素,我们选择自定义元素的名称。XML实际上不是语言,而是定义语言的标准(称为XML应用)。
元素名称区分大小写。元素可以包含其他元素,但不允许重叠(交叉)。

2、特性(属性)
可以在特性内存储数据,将特性添加到元素的开始标记内
特性的形式为:name="value"
其中特性值必须包含在单引号或双引号内,
<book title="APUE"></bool>
<book title='APUE'></bool>
XML中两种储存数据的方式
<book>
<title>APUE</title>
<bool>
<book title="APUE"></bool>
两者没太大区别

3、XML声明
XML文档的各个组成部分称为节点---因此元素、元素内的文本和特性都是XML文档的节点。
XML声明的格式类似于元素,但是在见括号内有问号。它的名称始终都是xml。并总是有version特性;当前,它只有两个值:1.0和1.1,但VS不支持1.1。XML还可以包含encoding和standalone特性,但这不是必须的。
<?xml version="1.0" encoding="utf-8" ?>
4、XML文档的结构
XML文档的数据分层组织,有且仅有一个根元素,其不需要任何域定义的结构。
5、XML名称空间
XML可以使用XML名称空间定义XML词汇表(作用类比C#中的命名空间)。
6、验证XML文档
XML支持通过两种方式,来定义在文档中可以放置那些元素和特性,及其放置的顺序——文档类型定义(Document Type Definitions, DTD)和模式(Schema)。
1)DTD
DTD使用从XML的父文档继承的feiXML语法,并逐渐被模式(schema)所替代。DTD不允许规定元素和属性的数据类型,使用不太灵活。
2)模式
.NET支持的模式具有两种不同格式——XML Schema Definition语言(XSD)和XML Data Reduced模式(XDR)。
有些复杂,后面研究下。


二、XML应用

1、XML文档对象模型(Document Object Model,DOM)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;using Microsoft.CSharp.RuntimeBinder;using System.Xml;namespace PartialFun{    public class LoginInfo    {        public string UserName { get; set; }        public string password { get; set; }        public int pwdType { get; set; }    }    class Program    {        static string ClassToXml(Object obj)        {                            if (obj == null)            {                return null;            }            string strXml = string.Empty;            string strDeclaration = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";            strXml += strDeclaration;            // 通过反射获取obj的属性信息            PropertyInfo[] pis = obj.GetType().GetProperties();            foreach (PropertyInfo pi in pis)            {                                    string strNode = string.Format("<{0}>{1}</{2}>",                     pi.Name, pi.GetValue(obj, null), pi.Name);                strXml += strNode;            }                               return strXml;        }        const string strFileName = @"test.xml";        static void WriteXml()        {            XmlDocument xmlDoc = new XmlDocument();            xmlDoc.Load(strFileName);           // XmlElement root = xmlDoc.CreateElement("Response");           // xmlDoc.AppendChild(root);            XmlElement root = xmlDoc.DocumentElement;            XmlElement xmlName = xmlDoc.CreateElement("UserName");            XmlText xmlValue = xmlDoc.CreateTextNode("hello, world");            xmlName.AppendChild(xmlValue);            root.AppendChild(xmlName);            xmlName = xmlDoc.CreateElement("Password");            xmlValue = xmlDoc.CreateTextNode("12345678");            xmlName.AppendChild(xmlValue);            root.AppendChild(xmlName);            xmlName = xmlDoc.CreateElement("PwdType");            xmlValue = xmlDoc.CreateTextNode("4");            xmlName.AppendChild(xmlValue);            root.AppendChild(xmlName);            Console.Write(xmlDoc.InnerXml);            xmlDoc.Save(strFileName);        }        static void Main(string[] args)        {#if TEST_FUNC            Base b = new Base();            b.Name = "All is well!";            Console.WriteLine(b.Name);            b = new Derived();            b.Name = "All is well!";            Console.WriteLine(b.Name);#endif            LoginInfo loginInfo = new LoginInfo();            loginInfo.UserName = "hello world";            loginInfo.password = "12345678";            loginInfo.pwdType = 4;            string strXml = ClassToXml(loginInfo);            Console.WriteLine(strXml);            WriteXml();            Console.ReadKey();        }    }}

0 0
原创粉丝点击