溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

第一章(asp.net xml與json)

發(fā)布時間:2020-07-17 14:35:16 來源:網(wǎng)絡(luò) 閱讀:325 作者:彳亍的路人 欄目:編程語言

                   第一章(asp.net xml與json)

1.html 是一種表現(xiàn)型的標(biāo)記語言;

2.xml 是可拓展的標(biāo)記語言;

3.xml寫法特點:

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

   (2)標(biāo)記必須關(guān)閉

   (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中將文件轉(zhuǎn)換為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.轉(zhuǎn)換到流:

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轉(zhuǎn)換成對象

   

//將json對象轉(zhuǎn)換為字符串
<script type="text/javascript">
    var jsonvar={"key","value"};
    //object類型
    alert(typeof.jsonvar);
    //string類型
    alert(typeof JSON.stringify(jsonvar));
<script>


   (2.)將json字符串轉(zhuǎn)換為json對象

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


13.json的優(yōu)點:提高可讀性,減少復(fù)雜性,

             json是完全動態(tài)的,允許在json結(jié)構(gòu)中間改變表示數(shù)據(jù)的方式,

             可以以不同的方式表示同一個json格式的對象.


14.xml與json的對比:

       (1.)客戶端:json(json格式易于處理) 優(yōu)于 xml

       (2)服務(wù)器:xml(xml在序列化和反序列化上更加穩(wěn)定) 優(yōu)于 json

       (3)安全性:xml 優(yōu)于 json(json需要正則表達式檢測)

       (4)性能:json(輕量級) 優(yōu)于 xml

       (5)其他:xml驗證技術(shù)更成熟.

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI