第一章(asp.net xml与json)

1.html 是一种表现型的标记语言;

2.xml 是可拓展的标记语言;

3.xml写法特点:

(1)<?xml version="1.0" encoding="utf-8" ?>

(2)标记必须关闭

(3)一个xml元素只有一个根元素

4.xml文件的写入:

XmlDocument doc = new XmlDocument();//创建xml文档描述XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);//创建根节点XmlNode root = doc.CreateNode(XmlNodeType.Element, "students", null);XmlNode node1 = doc.CreateNode(XmlNodeType.Element, "student", null);//文本节点XmlNode node1text = doc.CreateNode(XmlNodeType.Text, null, null);node1text.Value = "也许";//属性节点XmlAttribute attr1 = doc.CreateAttribute("hobby");attr1.Value = "唱歌";XmlAttribute attr2 = doc.CreateAttribute("age");attr1.Value = "20";//在node1节点添加文本节点node1.AppendChild(node1text);//在node1节点添加属性节点node1.Attributes.Append(attr1);node1.Attributes.Append(attr2);//在根节点添加子节点root.AppendChild(node1);//在文档中添加文档描述doc.AppendChild(declaration);//在文档中添加根节点doc.AppendChild(root);//保存文档string path =context.Server.MapPath(@"~/XML/yexu.xml");doc.Save(path);


5.xml的读取:

//实例化一个xmldocument XmlDocument doc = new XmlDocument(); string path = MapPath(@"~\XML\students.xml"); //加载xmldocumentdoc.Load(path); //得到文档的根节点 XmlNode root = doc.DocumentElement; string info = ""; //遍历根节点的子节点(找到<student>) foreach (XmlNode stuNode in root .ChildNodes) { info += stuNode.Attributes.Item(0).Value; info += stuNode.Attributes.Item(1).Value; //遍历stunode节点的子节点 foreach (XmlNode node in stuNode.ChildNodes ) { //得到节点的值 info += node.Value; } info += "<br/>"; } Response.Write(info);


6.xml操作:

(1)Xmldom XMLdocument ,xmlnode

(2)xmlreader ,xmlwritter [using system.xml.serialization]

(3)dataset readxml,writexml


7.在sql中将文件转换为xml: select * from [表名] for xml auto;

8.在js.jquery中:

(1.)① DomParser() firefox ,chrome

var DomParser() =new Domparser();

②ActiveXobject IE浏览器

var xml=new ActiveXobject("Microsoft.xmldom");

(2)jquery解析:

<script type ="text/javascript"> $(function () { $.ajax({ url: "http://localhost:2754/Handler1.ashx", type: "get", datatype: "xml", success: function (data) { $(data).each(function (index) { //读取文本节点 $(this).find("Student").text(); $(this).find("Student").each(function (attrindex) { //读取属性节点 $(this).get(0).attributes[0].value; }); }) } }) }) </script>


9.json解析:

<script type="text/javascript"> $(function () { $.ajax({ url: "JsonHandler.ashx", type: "get", dataType: "json", success: function (data) { $(data).each(function (index) { $(this)[0].bookname; }) } }) }); </script>


10.json:(序列化)

(1)JavaScriptSerializer

用法:list<book> list =new list<book>{new book(){bookid="",bookname=""}};

JavaScriptSerializer js =new JavaScriptSerializer();

js .Serializer(list);

注意:序列化返回string类型,不能解析dataset;


11.转换到流:

MemoryStream ms=new MemoryStream();xmldocument.save(ms);byte[] mybytes = byte[ms.length];mybytes = ms .ToArray();content.respone.outputStream.write(mybytes,0,mybytes.length);


12.ajaX数据上载:ajaxjson的使用

(1.)客户端将json转换成对象

//将json对象转换为字符串<script type="text/javascript"> var jsonvar={"key","value"}; //object类型 alert(typeof.jsonvar); //string类型 alert(typeof JSON.stringify(jsonvar));<script>


(2.)将json字符串转换为json对象

<script type="text/javascript"> var str={"key","value"}; Json.parse(str);<script>


13.json的优点:提高可读性,减少复杂性,

json是完全动态的,允许在json结构中间改变表示数据的方式,

可以以不同的方式表示同一个json格式的对象.


14.xml与json的对比:

(1.)客户端:json(json格式易于处理) 优于 xml

(2)服务器:xml(xml在序列化和反序列化上更加稳定) 优于 json

(3)安全性:xml 优于 json(json需要正则表达式检测)

(4)性能:json(轻量级) 优于 xml

(5)其他:xml验证技术更成熟.