第一章(asp.net xml與json)

                   第一章(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");
 //加載xmldocument
doc.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驗證技術更成熟.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章