第一章(asp.net xml與json)

色尼ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
1.html 是一種表現(xiàn)型的標記語言;
2.xml 是可拓展的標記語言;
3.xml寫法特點:
(1)<?xml version="1.0" encoding="utf-8" ?>
(2)標記必須關閉
(3)一個xml元素只有一個根元素
4.xml文件的寫入:
XmlDocument doc = new XmlDocument();
//創(chuàng)建xml文檔描述
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
//創(chuàng)建根節(jié)點
XmlNode root = doc.CreateNode(XmlNodeType.Element, "students", null);
XmlNode node1 = doc.CreateNode(XmlNodeType.Element, "student", null);
//文本節(jié)點
XmlNode node1text = doc.CreateNode(XmlNodeType.Text, null, null);
node1text.Value = "也許";
//屬性節(jié)點
XmlAttribute attr1 = doc.CreateAttribute("hobby");
attr1.Value = "唱歌";
XmlAttribute attr2 = doc.CreateAttribute("age");
attr1.Value = "20";
//在node1節(jié)點添加文本節(jié)點
node1.AppendChild(node1text);
//在node1節(jié)點添加屬性節(jié)點
node1.Attributes.Append(attr1);
node1.Attributes.Append(attr2);
//在根節(jié)點添加子節(jié)點
root.AppendChild(node1);
//在文檔中添加文檔描述
doc.AppendChild(declaration);
//在文檔中添加根節(jié)點
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");
//加載xmldocument
doc.Load(path);
//得到文檔的根節(jié)點
XmlNode root = doc.DocumentElement;
string info = "";
//遍歷根節(jié)點的子節(jié)點(找到<student>)
foreach (XmlNode stuNode in root .ChildNodes)
{
info += stuNode.Attributes.Item(0).Value;
info += stuNode.Attributes.Item(1).Value;
//遍歷stunode節(jié)點的子節(jié)點
foreach (XmlNode node in stuNode.ChildNodes )
{
//得到節(jié)點的值
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) {
//讀取文本節(jié)點
$(this).find("Student").text();
$(this).find("Student").each(function (attrindex) {
//讀取屬性節(jié)點
$(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數(shù)據(jù)上載: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的優(yōu)點:提高可讀性,減少復雜性,
json是完全動態(tài)的,允許在json結構中間改變表示數(shù)據(jù)的方式,
可以以不同的方式表示同一個json格式的對象.
14.xml與json的對比:
(1.)客戶端:json(json格式易于處理) 優(yōu)于 xml
(2)服務器:xml(xml在序列化和反序列化上更加穩(wěn)定) 優(yōu)于 json
(3)安全性:xml 優(yōu)于 json(json需要正則表達式檢測)
(4)性能:json(輕量級) 優(yōu)于 xml
(5)其他:xml驗證技術更成熟.
本文標題:第一章(asp.netxml與json)
標題路徑:http://www.chinadenli.net/article2/gccjic.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、App開發(fā)、營銷型網(wǎng)站建設、網(wǎng)站收錄、品牌網(wǎng)站設計、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)