XML 创建多个Element的XML文件

代码如下:

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>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值