.netcorecsharp 中级篇2-6
本节内容为json和xml操作
简介
json和xml文本是计算机网络通信中常见的文本格式,其中json其实就是javascript中的数组与对象,体现了一种面向对象的方式,而xml则是一种可标记语言,类似于我们的html标签,他更多的是体现一种层级关系。
但无论哪一种文本格式,我们都有学习的必要。
json
首先,介绍一下json:
json其实就是javascript里面的对象和数组,通过不同的组合,可以构成许多不同的数据结构。其中使用花括号的是对象,中括号的是数组,例如:
{ "data": { "people": [ {"name":"ppppp","age":18} ] }, "status":0 }
这里面,data就是一个对象,而people就是一个数组。
如果你要处理json数据,你在nuget上可以找到许多适宜的库,在这里,我是用的是litjson,这可能是一个比较少见的库,但是我觉得很好用。
这里我给出我们的免费api地址,这里你可以请求到我们的json文本。
对于litjson,它充分的阐明了我们json的核心——数组与对象。对于litjson,数组使用list ,对象则直接创建一个类进行处理。对于上面的例子,我们可以构造出如下的类关系
public class data { public list<people> people{ get; set; } public int status{ get; set; } } // 下面操作将json文本转换成data的对象 main() { jsoncontent jsonc = jsonmapper.toobject<data>(json); foreach(var t in data) }
更多内容可以看我的这篇博文json处理实战,以及litjson的官网
xml
xml也是广泛应用于网络信息交换的一种常见文本格式,他的书写有点类似于我们的html,正如之前所说,他更多的是阐明一种层级关系。例如下文便是一个常见的xml文本的格式。
<computers> <computer id="11111111" description="made in usa"> <name>surface</name> <price>5000</price> </computer> <computer id="2222222" description="made in usa"> <name>ibm</name> <price>10000</price> </computer> <computer id="3333333" description="made in usa"> <name>apple mac</name> <price>10000</price> </computer> </computers>
在c#中,我们操作xml同样的有许多库,这里我们使用xmldocument进行操作。
xmldocument类中的常用方法:
- load(string path)加载文件路径的xml
- selectsinglenode(string node)选中节点
- childnodes,属性不是函数,用于获取所有子节点,返回xmlnodelist对象
- haschildnodes属性,判断是否有子节点
- createelement创建新节点
- appendchild(xmlelement node)追加子节点
- insertbefore(xmlelement node,xmlelement childenodes)向前插入
- setattribute(string name,string value)为指定节点的新建属性并赋值
- innertext属性,获取内部文本
- save()保存
xmldocument myxmldoc = new xmldocument(); //加载xml文件(参数为xml文件的路径) myxmldoc.load(xmlfilepath); //获得第一个姓名匹配的节点(selectsinglenode):此xml文件的根节点 xmlnode rootnode = myxmldoc.selectsinglenode("computers"); //分别获得该节点的innerxml和outerxml信息 string innerxmlinfo = rootnode.innerxml.tostring(); string outerxmlinfo = rootnode.outerxml.tostring(); //获得该节点的子节点(即:该节点的第一层子节点) xmlnodelist firstlevelnodelist = rootnode.childnodes; foreach (xmlnode node in firstlevelnodelist) { //获得该节点的属性集合 xmlattributecollection attributecol = node.attributes; foreach (xmlattribute attri in attributecol) { //获取属性名称与属性值 string name = attri.name; string value = attri.value; console.writeline("{0} = {1}", name, value); } //判断此节点是否还有子节点 if (node.haschildnodes) { //获取该节点的第一个子节点 xmlnode secondlevelnode1 = node.firstchild; //获取该节点的名字 string name = secondlevelnode1.name; //获取该节点的值(即:innertext) string innertext = secondlevelnode1.innertext; console.writeline("{0} = {1}", name, innertext); //获取该节点的第二个子节点(用数组下标获取) xmlnode secondlevelnode2 = node.childnodes[1]; name = secondlevelnode2.name; innertext = secondlevelnode2.innertext; console.writeline("{0} = {1}", name, innertext); } } } catch (exception ex) { console.writeline(ex.tostring()); }
博主不常用xml,更多内容请参考微软官方文档以及https://www.cnblogs.com/zhengwei-cq/p/7242979.html的这篇博文
如果我的文章帮助了您,请您在github.netcoreguide项目帮我点一个star,在博客园中点一个关注和推荐。
github
bilibili主页
warrenryan’sblog