XML 创建多个Element的XML文件
2017-04-24 11:15
211 查看
代码如下:
结果如下:
<?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>
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>
相关文章推荐
- android studio创建多个activity时在AndroidManifest.xml文件中配置细节
- 使用 Asp 创建 XML 文件
- VBScript 使用 xmldom 检测/创建/读取/更改 XML 文件数据 实例 By shawl.qiu
- 创建存储过程的XML注释文件
- 如何使用PHP DOM创建动态的XML文件
- 利用Hibernate工具,通过xml文件自动创建表
- 简单的XML操作:XML文件创建
- 创建一个xml文件(c:/test.html),为该文件增加三个节点,,,将b的attribute改为4并保存
- 创建、查询、修改带名称空间的 XML 文件的例子
- 一个创建XMLHttpRequest对象的js文件(适用asp,php,jsp,asp.net等)
- 用xmldocument创建一个Xml文件
- 创建带有关联的 XML 架构的 XML 文件 && 从 XML 文件创建 XML 架构
- 创建、查询、修改带名称空间的 XML 文件的例子
- [导入]创建、查询、修改带名称空间的 XML 文件的例子
- 创建、查询、修改带名称空间的 XML 文件的例子
- 创建存储过程的XML注释文件
- 使用PHP DOM-XML创建和解析XML文件
- [导入]创建、查询、修改带名称空间的 XML 文件的例子
- asp.net下创建、查询、修改带名称空间的 XML 文件的例子
- 用 TClientDataSet 创建 XML 文件的过程 - 回复"丁永其"的问题