您的位置:首页 > 移动开发 > Unity3D

Unity3D研究院之使用 C#合成解析XML与JSON

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





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

C#

1

2

3

4

5

6

7

usingUnityEngine;

usingSystem.Collections;

usingSystem.Collections.Generic;

usingSystem.Xml;

usingSystem.IO;

usingSystem.Text;

usingLitJson;

1、生成XML

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

publicvoidcreateXml()

{

//xml保存的路径,这里放在Assets路径
注意路径。

stringfilepath=Application.dataPath+@"/my.xml";

//继续判断当前路径下是否有该文件

if(!File.Exists(filepath))

{

//创建XML文档实例

XmlDocumentxmlDoc=newXmlDocument();

//创建root节点,也就是最上一层节点

XmlElementroot=xmlDoc.CreateElement("transforms");

//继续创建下一层节点

XmlElementelmNew=xmlDoc.CreateElement("rotation");

//设置节点的两个属性
ID 和 NAME

elmNew.SetAttribute("id","0");

elmNew.SetAttribute("name","momo");

//继续创建下一层节点

XmlElementrotation_X=xmlDoc.CreateElement("x");

//设置节点中的数值

rotation_X.InnerText="0";

XmlElementrotation_Y=xmlDoc.CreateElement("y");

rotation_Y.InnerText="1";

XmlElementrotation_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!");

}

}

运行结果

C#

1

2

3

4

5

6

7

<transforms>

<rotationid="0"name="momo">

<x>0</x>

<y>1</y>

<zid="1">2</z>

</rotation>

</transforms>

2.更新XML文件

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

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

publicvoidUpdateXml()

{

stringfilepath=Application.dataPath+@"/my.xml";

if(File.Exists(filepath))

{

XmlDocumentxmlDoc=newXmlDocument();

//根据路径将XML读取出来

xmlDoc.Load(filepath);

//得到transforms下的所有子节点

XmlNodeListnodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;

//遍历所有子节点

foreach(XmlElementxeinnodeList)

{

//拿到节点中属性ID
=0的节点

if(xe.GetAttribute("id")=="0")

{

//更新节点属性

xe.SetAttribute("id","1000");

//继续遍历

foreach(XmlElementx1inxe.ChildNodes)

{

if(x1.Name=="z")

{

//这里是修改节点名称对应的数值,而上面的拿到节点连带的属性。。。

x1.InnerText="update00000";

}

}

break;

}

}

xmlDoc.Save(filepath);

Debug.Log("UpdateXml
OK!");

}

}

运行结果

C#

1

2

3

4

5

6

7

<transforms>

<rotationid="1000"name="momo">

<x>0</x>

<y>1</y>

<zid="1">update00000</z>

</rotation>

</transforms>

3.添加XML

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

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

publicvoidAddXml()

{

stringfilepath=Application.dataPath+@"/my.xml";

if(File.Exists(filepath))

{

XmlDocumentxmlDoc=newXmlDocument();

xmlDoc.Load(filepath);

XmlNoderoot=xmlDoc.SelectSingleNode("transforms");

XmlElementelmNew=xmlDoc.CreateElement("rotation");

elmNew.SetAttribute("id","1");

elmNew.SetAttribute("name","yusong");

XmlElementrotation_X=xmlDoc.CreateElement("x");

rotation_X.InnerText="0";

rotation_X.SetAttribute("id","1");

XmlElementrotation_Y=xmlDoc.CreateElement("y");

rotation_Y.InnerText="1";

XmlElementrotation_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!");

}

}

运行结果

C#

1

2

3

4

5

6

7

8

9

10

11

12

<transforms>

<rotationid="1000"name="momo">

<x>0</x>

<y>1</y>

<zid="1">update00000</z>

</rotation>

<rotationid="1"name="yusong">

<xid="1">0</x>

<y>1</y>

<z>2</z>

</rotation>

</transforms>

4.删除XML

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

publicvoiddeleteXml()

{

stringfilepath=Application.dataPath+@"/my.xml";

if(File.Exists(filepath))

{

XmlDocumentxmlDoc=newXmlDocument();

xmlDoc.Load(filepath);

XmlNodeListnodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;

foreach(XmlElementxeinnodeList)

{

if(xe.GetAttribute("id")=="1")

{

xe.RemoveAttribute("id");

}

foreach(XmlElementx1inxe.ChildNodes)

{

if(x1.Name=="z")

{

x1.RemoveAll();

}

}

}

xmlDoc.Save(filepath);

Debug.Log("deleteXml
OK!");

}

}

运行结果

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<transforms>

<rotationid="1000"name="momo">

<x>0</x>

<y>1</y>

<z>

</z>

</rotation>

<rotationname="yusong">

<xid="1">0</x>

<y>1</y>

<z>

</z>

</rotation>

</transforms>

4.解析与输出上面的XML

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

publicvoidshowXml()

{

stringfilepath=Application.dataPath+@"/my.xml";

if(File.Exists(filepath))

{

XmlDocumentxmlDoc=newXmlDocument();

xmlDoc.Load(filepath);

XmlNodeListnodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;

//遍历每一个节点,拿节点的属性以及节点的内容

foreach(XmlElementxeinnodeList)

{

Debug.Log("Attribute
:"+xe.GetAttribute("name"));

Debug.Log("NAME
:"+xe.Name);

foreach(XmlElementx1inxe.ChildNodes)

{

if(x1.Name=="y")

{

Debug.Log("VALUE
:"+x1.InnerText);

}

}

}

Debug.Log("all
= "+xmlDoc.OuterXml);

}

}

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





接着是处理JSON

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

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

publicvoidResolveJson()

{

//定义的JSON字符串,注意JSON的格式

stringstr=@"

{

""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""

}

]

}";

//这里是解析,包括整形与字符串

JsonDatajd=JsonMapper.ToObject(str);

Debug.Log("name
= "+(string)jd["Name"]);

Debug.Log("Age
= "+(int)jd["Age"]);

Debug.Log("Birthday
= "+(string)jd["Birthday"]);

JsonDatajdItems=jd["Thumbnail"];

for(inti=0;i<jdItems.Count;i++)

{

Debug.Log("URL
= "+jdItems[i]["Url"]);

Debug.Log("Height
= "+(int)jdItems[i]["Height"]);

Debug.Log("Width
= "+jdItems[i]["Width"]);

}

}

运行结果





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

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

publicvoidMergerJson()

{

StringBuildersb=newStringBuilder();

JsonWriterwriter=newJsonWriter(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());

JsonDatajd=JsonMapper.ToObject(sb.ToString());

Debug.Log("name
= "+(string)jd["Name"]);

Debug.Log("Age
= "+(int)jd["Age"]);

JsonDatajdItems=jd["Girl"];

for(inti=0;i<jdItems.Count;i++)

{

Debug.Log("Girl
name = "+jdItems[i]["name"]);

Debug.Log("Girl
age = "+(int)jdItems[i]["age"]);

}

}

运行结果





工程下载: http://vdisk.weibo.com/s/jkBml
本文转自雨松MOMO
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息