blog games developers documentation portfolio gallery

More in this category:

XmlString()

To export a Hashtable (with underlying hierarchy) or ArrayList to XML, you can call the class extensions for Hashtable and ArrayList. You need to include the line
using OrbCreationExtensions;
in your source code.

string XmlString()


This function is defined for Hashtable and for ArrayList and returns a XML formatted string.

Since the XmlString() function only creates a string, here's an example of how to write the string to a file:

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

using SimpleJsonExtensions;

public class MyExporter {

public void ExportXml(Hashtable aHash) {
string xmlExportString = aHash.XmlString();
ExportToFile(xmlExportString, "SimpleXmlExport.xml");
}

private void ExportToFile(string exportString, string path) {
if(exportString == null || path == null) return;
if(Application.platform==RuntimePlatform.OSXPlayer ||
Application.platform==RuntimePlatform.WindowsPlayer &&
Application.platform!=RuntimePlatform.LinuxPlayer || Application.isEditor) {

// Put a proper prolog in the file
exportString = "\n" + exportString;

// We use UTF-8 encoding
byte[] bytes = new UTF8Encoding(true).GetBytes(exportString);
if(File.Exists(path)) File.Delete(path); // delete if it exists
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
BinaryWriter w = new BinaryWriter(fs);
w.Write(bytes);
w.Close();
fs.Close();
}
}
}


If you have created your own hierachy of Hashtables and ArrayLists, make sure that the elements of your arraylists always contain a "tag" property if they have more than 1 property.

This is fine:
{
level1: [
{
level2: Somevalue,
},
{
level2: Anothervalue,
},
],
}

Exported to XML:

Somevalue
Anothervalue



This is not, because there is no tag name to identify the elements of the array:
{
level1: [
{
level2a: Somevalue,
level2b: Anothervalue,
},
{
level2a: Yetanothervalue,
level2b: Runningoutofvalues,
},
],
}


You should change this to:
{
level1: [
{
tag: level2,
level2a: Somevalue,
level2b: Anothervalue,
},
{
tag: level2,
level2a: Yetanothervalue,
level2b: Runningoutofvalues,
},
],
}

Exported to XML:


Somevalue
Anothervalue


Yetanothervalue
Runningoutofvalues









follow us