当前位置: 首页 > news >正文

惠州seo外包服务/南京seo网站优化

惠州seo外包服务,南京seo网站优化,140平米装修全包费用,专业做app下载网站有哪些XML与JSON在开发中非常重要, 其实核心就是处理字符串。一个是XML的字符串一个是JSON的字符串,尤其是在处理网络请求的时候,肯定是要用的。另外现在JSON非常的流行,我写了一个简单的例子融合了XML与JSON的合成与解析,希…

 XML与JSON在开发中非常重要, 其实核心就是处理字符串。一个是XML的字符串一个是JSON的字符串,尤其是在处理网络请求的时候,肯定是要用的。另外现在JSON非常的流行,我写了一个简单的例子融合了XML与JSON的合成与解析,希望大家喜欢!

 

Unity3D研究院之使用 C#合成解析XML与JSON(四十一) - 雨松MOMO程序研究院 - 1

首先注意头文件,LitJson是处理JSON的第三方库,最后我会给出下载地址。

 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;
using LitJson;

  1、生成XML

public void createXml(){//xml保存的路径,这里放在Assets路径 注意路径。string filepath = Application.dataPath + @"/my.xml";//继续判断当前路径下是否有该文件if(!File.Exists (filepath)){//创建XML文档实例XmlDocument xmlDoc = new XmlDocument();//创建root节点,也就是最上一层节点XmlElement root = xmlDoc.CreateElement("transforms");//继续创建下一层节点XmlElement elmNew = xmlDoc.CreateElement("rotation");//设置节点的两个属性 ID 和 NAMEelmNew.SetAttribute("id","0");elmNew.SetAttribute("name","momo");//继续创建下一层节点XmlElement rotation_X = xmlDoc.CreateElement("x");//设置节点中的数值rotation_X.InnerText = "0";XmlElement rotation_Y = xmlDoc.CreateElement("y");rotation_Y.InnerText = "1";XmlElement rotation_Z = xmlDoc.CreateElement("z");rotation_Z.InnerText = "2";//这里在添加一个节点属性,用来区分。。rotation_Z.SetAttribute("id","1");//把节点一层一层的添加至XMLDoc中 ,请仔细看它们之间的先后顺序,这将是生成XML文件的顺序elmNew.AppendChild(rotation_X);elmNew.AppendChild(rotation_Y);elmNew.AppendChild(rotation_Z);root.AppendChild(elmNew);xmlDoc.AppendChild(root);//把XML文件保存至本地xmlDoc.Save(filepath);Debug.Log("createXml OK!");}}

  运行结果

<transforms><rotation id="0" name="momo"><x>0</x><y>1</y><z id="1">2</z></rotation>
</transforms>

  

2.更新XML文件

以其中某个节点名称做条件,当查询到时更新该节点

public void UpdateXml(){string filepath = Application.dataPath + @"/my.xml";if(File.Exists (filepath)){XmlDocument xmlDoc = new XmlDocument();//根据路径将XML读取出来xmlDoc.Load(filepath);//得到transforms下的所有子节点XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;//遍历所有子节点foreach(XmlElement xe in nodeList){//拿到节点中属性ID =0的节点if(xe.GetAttribute("id")=="0"){//更新节点属性xe.SetAttribute("id","1000");//继续遍历foreach(XmlElement x1 in xe.ChildNodes){if(x1.Name=="z"){//这里是修改节点名称对应的数值,而上面的拿到节点连带的属性。。。x1.InnerText="update00000";}}break;}}xmlDoc.Save(filepath);Debug.Log("UpdateXml OK!");}}
<transforms><rotation id="1000" name="momo"><x>0</x><y>1</y><z id="1">update00000</z></rotation>
</transforms>

  运行结果↑

 

 

3.添加XML

重复的地方我就不解释拉。

public void AddXml(){string filepath = Application.dataPath + @"/my.xml";if(File.Exists (filepath)){XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(filepath);XmlNode root = xmlDoc.SelectSingleNode("transforms");XmlElement elmNew = xmlDoc.CreateElement("rotation");elmNew.SetAttribute("id","1");elmNew.SetAttribute("name","yusong");XmlElement rotation_X = xmlDoc.CreateElement("x");rotation_X.InnerText = "0";rotation_X.SetAttribute("id","1");XmlElement rotation_Y = xmlDoc.CreateElement("y");rotation_Y.InnerText = "1";XmlElement rotation_Z = xmlDoc.CreateElement("z");rotation_Z.InnerText = "2"; elmNew.AppendChild(rotation_X);elmNew.AppendChild(rotation_Y);elmNew.AppendChild(rotation_Z);root.AppendChild(elmNew);xmlDoc.AppendChild(root);xmlDoc.Save(filepath);Debug.Log("AddXml OK!");}}

  运行结果

<transforms><rotation id="1000" name="momo"><x>0</x><y>1</y><z id="1">update00000</z></rotation><rotation id="1" name="yusong"><x id="1">0</x><y>1</y><z>2</z></rotation>
</transforms>

  4.删除XML

public void deleteXml(){string filepath = Application.dataPath + @"/my.xml";if(File.Exists (filepath)){XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(filepath);XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;foreach(XmlElement xe in nodeList){if(xe.GetAttribute("id")=="1"){xe.RemoveAttribute("id");}foreach(XmlElement x1 in xe.ChildNodes){if(x1.Name == "z"){x1.RemoveAll();}}}xmlDoc.Save(filepath);Debug.Log("deleteXml OK!");}}

  运行结果

<transforms><rotation id="1000" name="momo"><x>0</x><y>1</y><z></z></rotation><rotation name="yusong"><x id="1">0</x><y>1</y><z></z></rotation>
</transforms>

  4.解析与输出上面的XML

	public void showXml(){string filepath = Application.dataPath + @"/my.xml";if(File.Exists (filepath)){XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(filepath);XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;//遍历每一个节点,拿节点的属性以及节点的内容foreach(XmlElement xe in nodeList){Debug.Log("Attribute :" + xe.GetAttribute("name"));Debug.Log("NAME :" + xe.Name);foreach(XmlElement x1 in xe.ChildNodes){if(x1.Name == "y"){Debug.Log("VALUE :" + x1.InnerText);}}}Debug.Log("all = " + xmlDoc.OuterXml);}}

  

运行结果(点击图片最大化)

Unity3D研究院之使用 C#合成解析XML与JSON(四十一) - 雨松MOMO程序研究院 - 2

 

接着是处理JSON

5.解析JSON字符串显示字典键值

	public void ResolveJson(){//定义的JSON字符串,注意JSON的格式string str = @"{""Name""     : ""yusong"",""Age""      : 26,""Birthday"" : ""1986-11-21"",""Thumbnail"":[{""Url"":    ""http://xuanyusong.com"",""Height"": 256,""Width"":  ""200""},{""Url"":    ""http://baidu.com"",""Height"": 1024,""Width"":  ""500""}]}";//这里是解析,包括整形与字符串JsonData jd = JsonMapper.ToObject(str);Debug.Log("name = " + (string)jd["Name"]);Debug.Log("Age = " + (int)jd["Age"]);Debug.Log("Birthday = " + (string)jd["Birthday"]);JsonData jdItems = jd["Thumbnail"]; for (int i = 0; i < jdItems.Count; i++){Debug.Log("URL = " + jdItems[i]["Url"]);Debug.Log("Height = " + (int)jdItems[i]["Height"]);Debug.Log("Width = " + jdItems[i]["Width"]);}}

  

运行结果

Unity3D研究院之使用 C#合成解析XML与JSON(四十一) - 雨松MOMO程序研究院 - 3

 

6.合成JSON字符串,先合成 然后在输出。

	public void MergerJson(){StringBuilder sb = new StringBuilder ();JsonWriter writer = new JsonWriter (sb);writer.WriteObjectStart ();writer.WritePropertyName ("Name");writer.Write ("yusong");writer.WritePropertyName ("Age");writer.Write (26);writer.WritePropertyName ("Girl");writer.WriteArrayStart ();writer.WriteObjectStart();writer.WritePropertyName("name");writer.Write("ruoruo");writer.WritePropertyName("age");writer.Write(24);writer.WriteObjectEnd ();writer.WriteObjectStart();writer.WritePropertyName("name");writer.Write("momo");writer.WritePropertyName("age");writer.Write(26);writer.WriteObjectEnd ();writer.WriteArrayEnd();writer.WriteObjectEnd ();Debug.Log(sb.ToString ());JsonData jd = JsonMapper.ToObject(sb.ToString ());Debug.Log("name = " + (string)jd["Name"]);Debug.Log("Age = " + (int)jd["Age"]);JsonData jdItems = jd["Girl"];for (int i = 0; i < jdItems.Count; i++){Debug.Log("Girl name = " + jdItems[i]["name"]);Debug.Log("Girl age = " + (int)jdItems[i]["age"]);}}

  

运行结果

Unity3D研究院之使用 C#合成解析XML与JSON(四十一) - 雨松MOMO程序研究院 - 4

 

 http://www.xuanyusong.com/archives/1901

转载 雨松MOMO 2012年11月29日 于 雨松MOMO程序研究院 发表

转载于:https://www.cnblogs.com/rongweijun/p/6499226.html

http://www.jmfq.cn/news/5254687.html

相关文章:

  • 西安做网站找缑阳建/腾讯广告推广平台入口
  • 企业 网站微信 建设/莆田百度推广开户
  • wordpress编辑器段间距/优化网站内容的方法
  • 福田网站建设设计/厦门seo俱乐部
  • 唐山哪里有做网站的/晋城网站seo
  • 关于旅游网站策划书/国内最新新闻
  • 施坦威网站关于我们/深圳百度推广电话
  • 网站研发公司/windows优化大师要钱
  • 湛江专业看房/漯河seo推广
  • 2017湖北建设教育协会网站/最全bt搜索引擎入口
  • 阿里云云服务器ecs做网站访问慢/企业文化标语经典
  • 哈尔滨大型网站设计公司/官方网站营销
  • 查询类网站用什么做/seo查询官方网站
  • 百度推广会帮你做网站不/国际最新新闻热点事件
  • 网站文章结构变更怎么做301/企业推广语
  • 网站发语音功能如何做/线下推广怎么做
  • 有赞做网站/首页排名seo
  • 做一家视频网站/东莞网站制作推广公司
  • 加人引流加人网站怎么做/企业网站建设cms
  • 科技布沙发/网站整站优化推广方案
  • 怎么做跳转网站 充值登陆/seo搜索引擎推广
  • 中国电力建设集团有限公司/百度推广优化是什么?
  • 自己怎么给网站做优化/seo推广软件哪个好
  • 自己做抽奖网站违法/软文代写价格
  • 广告设计制作专业/关键词优化的发展趋势
  • 江苏省住房和城乡建设厅网站首页/整站seo
  • gravatar wordpress 禁用/seo深圳网络推广
  • 网站建设 管理与维护试题/谷歌浏览器网页
  • 网站建设 cn/百度推广客服电话24小时
  • 玩具公司网站开发论文/手机百度账号登录入口