您的位置:首页 > 其它

XML 创建多个Element的XML文件

2017-04-24 11:15 211 查看
代码如下:

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

public class XmlDoc : MonoBehaviour {

string savePath = Directory.GetCurrentDirectory () + "/data.xml";

// Use this for initialization
void Start () {
bool success = CreateXmlDoc ();
if (success)
{
Debug.Log ("文件保存成功!");
}
else
{
Debug.Log ("文件保存失败!");
}
}

bool CreateXmlDoc()
{

if (File.Exists (savePath))
{
File.Delete(savePath);
}

XmlDocument doc = new XmlDocument();

//加入XML的声明段落,<?xml version="1.0" encoding="UTF-8"?>
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(xmldecl);

try
{
XmlElement root = doc.CreateElement("adsconfig");
doc.AppendChild(root);

XmlNode rootNode = doc.SelectSingleNode("adsconfig");

XmlElement adsshopElem = doc.CreateElement("adsshop");
rootNode.AppendChild(adsshopElem);

XmlElement adspackagegiftElem = doc.CreateElement("adspackagegift");
rootNode.AppendChild(adspackagegiftElem);

XmlElement guide_adsElem = doc.CreateElement("guide_ads");
rootNode.AppendChild(guide_adsElem);

XmlElement advertising_listElem = doc.CreateElement("advertising_list");
rootNode.AppendChild(advertising_listElem);

//创建节点
XmlNode ads = root.SelectSingleNode("adsshop");
ads.RemoveAll();

for (int i=0; i<3; i++)
{
XmlNode ad = doc.CreateElement("ad");
ads.AppendChild(ad);

XmlAttribute at = doc.CreateAttribute("source");
at.Value = "pcweb";
ad.Attributes.Append(at);

at = doc.CreateAttribute("img");
at.Value = "http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/1028x143jn20170327026.jpg";
ad.Attributes.Append(at);

at = doc.CreateAttribute("type");
at.Value = "1";
ad.Attributes.Append(at);

at = doc.CreateAttribute("value");
at.Value = "http://5s.qq.com/cp/a20170316taoh";
ad.Attributes.Append(at);
}

ads = root.SelectSingleNode("adspackagegift");
ads.RemoveAll();

for (int i=0; i<3; i++)
{
XmlNode ad = doc.CreateElement("ad");
ads.AppendChild(ad);

XmlAttribute at = doc.CreateAttribute("source");
at.Value = "pcweb";
ad.Attributes.Append(at);

at = doc.CreateAttribute("url");
at.Value = "http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/1028x143jn20170327026.jpg";
ad.Attributes.Append(at);

at = doc.CreateAttribute("relocation");
at.Value = "http://5s.qq.com/cp/a20170316taoh";
ad.Attributes.Append(at);
}

ads = root.SelectSingleNode("guide_ads");
ads.RemoveAll();

for (int i=0; i<3; i++)
{
XmlNode ad = doc.CreateElement("ad");
ads.AppendChild(ad);

XmlAttribute at = doc.CreateAttribute("type");
at.Value = "2";
ad.Attributes.Append(at);

at = doc.CreateAttribute("source");
at.Value = "pcweb";
ad.Attributes.Append(at);

at = doc.CreateAttribute("url");
at.Value = "http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/350x256jn2017032703.jpg";
ad.Attributes.Append(at);

at = doc.CreateAttribute("relocation");
at.Value = "http://5s.qq.com/cp/a20170316taoh";
ad.Attributes.Append(at);
}

ads = root.SelectSingleNode("advertising_list");
ads.RemoveAll();

for (int i=0; i<3; i++)
{
XmlNode ad = doc.CreateElement("advertising");
ads.AppendChild(ad);

XmlAttribute at = doc.CreateAttribute("type");
at.Value = "2";
ad.Attributes.Append(at);

at = doc.CreateAttribute("picture");
at.Value = "http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/350-341jn2017032713.jpg";
ad.Attributes.Append(at);

at = doc.CreateAttribute("url");
at.Value = "http://5s.qq.com/cp/a20170323syhk/index.htm";
ad.Attributes.Append(at);
}

doc.Save(savePath);
}
catch(System.Exception ex)
{
Debug.LogError(ex.Message);
return false;
}
return true;
}

// Update is called once per frame
void Update () {

}
}


结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<adsconfig>
<adsshop>
<ad source="pcweb" img="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/1028x143jn2017.jpg" type="1" value="http://5s.qq.com/cp/a20170316taoh" />
<ad source="pcweb" img="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/1028x143jn2017.jpg" type="1" value="http://5s.qq.com/cp/a20170316taoh" />
<ad source="pcweb" img="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/1028x143jn2017.jpg" type="1" value="http://5s.qq.com/cp/a20170316taoh" />
</adsshop>
<adspackagegift>
<ad source="pcweb" url="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/1028x143jn2017.jpg" relocation="http://5s.qq.com/cp/a20170316taoh" />
<ad source="pcweb" url="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/1028x143jn2017.jpg" relocation="http://5s.qq.com/cp/a20170316taoh" />
<ad source="pcweb" url="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/1028x143jn2017.jpg" relocation="http://5s.qq.com/cp/a20170316taoh" />
</adspackagegift>
<guide_ads>
<ad type="2" source="pcweb" url="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/350x256.jpg" relocation="http://5s.qq.com/cp/a20170316taoh" />
<ad type="2" source="pcweb" url="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/350x256.jpg" relocation="http://5s.qq.com/cp/a20170316taoh" />
<ad type="2" source="pcweb" url="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/350x256.jpg" relocation="http://5s.qq.com/cp/a20170316taoh" />
</guide_ads>
<advertising_list>
<advertising type="2" picture="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/350-341.jpg" url="http://5s.qq.com/cp/a20170323syhk/index.htm" />
<advertising type="2" picture="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/350-341.jpg" url="http://5s.qq.com/cp/a20170323syhk/index.htm" />
<advertising type="2" picture="http://ossweb-img.qq.com/images/chanpin/5s/v_ckzhong/350-341.jpg" url="http://5s.qq.com/cp/a20170323syhk/index.htm" />
</advertising_list>
</adsconfig>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: