第一章(asp.net xml与json)

色尼ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
1.html 是一种表现型的标记语言;
2.xml 是可拓展的标记语言;
3.xml写法特点:
(1)
(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 = ""; //遍历根节点的子节点(找到) 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 += "
"; } 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解析:
9.json解析:
10.json:(序列化)
(1)JavaScriptSerializer
用法:list
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对象转换为字符串


咨询
建站咨询