Serialize an array from all items inside of XML root.

Hi,

As described in this post, I want to use the Google Play App Translation Service in my Unity game. So, I tried XML Serialization to do it myself. I’m getting a problem though. To serialize an XML array you need

[XmlRoot("root")]
public class Root
{
[XmlArray("array"), XmlArrayItem("string")]
    public Array[] strings;
}
public class Array
{
    //Attributes, text, etc.,
}

“strings.xml”, what you submit to the Google Play App Translation Service has a form of

<?xml version="1.0" encoding="utf-8"?>
<root>
  <string name="test_text">Test</string>
  <string name="clear_text">Clear</string>
  <string name="etc">Insert text here.</string>
</root>

The tag for an array is missing in the XML, “XmlRoot” in C# cannot be used with an array and “XmlArray” in C# cannot be used with a class. And Google rejects your XML file if it’s in any other form than what is listed above. I’ve tried a lot of things but nothing seems to work. I’m almost wondering if I need a script to insert an array tag in the XML so it can be serialized, and then take it out once it’s done. Ideas?

If you need to see any of my actual code, I’ll be glad to upload it.

Thanks in advance;).

To correct myself, I needed deserialization, not serialization.

I actually was able to load the XML file, but not save it (I used Resources.Load because I am using Android). That’s okay, because saving is unnecessary for me right now. I loaded the text from the XML in an XmlDocument, added a node, then deserialized it as a string. Here’s my code.

using System.Xml;
using System.Xml.Serialization;
using System.IO;
using UnityEngine;

[XmlRoot("resources")]
public class StringsContainer
{
    [XmlArray("res"), XmlArrayItem("string")]
    public String[] Strings;

    public static StringsContainer Load(string path)
    {
        XmlDocument xmlDoc = new XmlDocument();
        string xmlData = (Resources.Load(path) as TextAsset).text;
        xmlDoc.LoadXml(xmlData); //Loads the XML into the newly created XmlDocument.

        XmlNode root = xmlDoc.DocumentElement; //Finds the root.

        XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "res", "");
        root.InsertBefore(node, root.FirstChild); //Creates an unecessary node for deserialization.

        if (root.HasChildNodes)
        {
            for (int i = 0; i < root.ChildNodes.Count; i++)
            {
                XmlNode child = root.ChildNodes[i].Clone();
                node.AppendChild(child); //Copies each string to the new node.
            }
            XmlNodeList list = root.ChildNodes;

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Name != "res")
                {
                    root.RemoveChild(list[i]); //Deletes each old string from the root.
                    i--;
                }
            }
        }
        using (var stringWriter = new StringWriter())
        using (var xmlTextWriter = XmlWriter.Create(stringWriter))
        {
            xmlDoc.WriteTo(xmlTextWriter);
            xmlTextWriter.Flush();
           
            var serializer = new XmlSerializer(typeof(StringsContainer)); //Deserializes the XmlDocument as a string.
            return serializer.Deserialize(new StringReader(stringWriter.GetStringBuilder().ToString())) as StringsContainer;
        }
    }
}

public class String
{
    [XmlAttribute("name")]
    public string name;

    [XmlText(Type = typeof(string))]
    public string text;
}

I hope this helps someone!