IU Prefabs list from xml string, Help me

Hi, I am days that I try in every way to make a list of messages, but I can not imagine the mechanism.

Then I created a php function that draws from the message list and returns this structure in xml:

<messages>
<count value="2">
<message>
<idmsg>1</idmsg>
<sender>Morpheus Tao</sender>
<title>Title text message</title>
<body>Message</body>
<readed>0</readed>
<date>2015-09-06 01:52:30</date>
</message>
<message>
<idmsg>2</idmsg>
<sender>Tirion Namir</sender>
<title>Title text message</title>
<body>Message</body>
<readed>0</readed>
<date>2015-09-06 01:52:30</date>
</message>
</count>
</messages>

With this function:

public IEnumerator LoadMessage(string username){
        string MessageUrl= config.ServerUri + "Etc....";
        var www = new WWW (MessageUrl);
       
        if (!www.isDone)
            yield return www;
       
        if (!string.IsNullOrEmpty (www.error)) {
            Debug.LogException(new Exception("NetworkManager.Messages error" + www.error));
            IsLoggedIn = true;
            yield break;
        }
       
        Debug.Log ("result:" + www.text);
       
        XmlDocument xmlDoc = new XmlDocument ();
        xmlDoc.LoadXml (www.text);
        int count = 0;
        XmlNodeList xnl = xmlDoc.SelectNodes("/messages/count");
        foreach (XmlNode node in xnl)
        {
            Debug.Log( count = Convert.ToInt32(node.Attributes["value"].InnerText));
            if (count != 0)
            {
                XmlNodeList xnl2 = xmlDoc.SelectNodes("/messages/count/message");
                foreach (XmlNode node2 in xnl2)
                {
                  //Test Debug info loaded
                    Debug.Log(node2["idmsg"].InnerText);
                    Debug.Log(node2["sender"].InnerText);
                    Debug.Log(NetworkSecurity.Decrypt(node2["title"].InnerText));
                    Debug.Log(NetworkSecurity.Decrypt(node2["body"].InnerText));
                    Debug.Log(node2["readed"].InnerText);
                    Debug.Log(node2["date"].InnerText);
                }
            }

        }
       
       
    }

Now I would like the messages loaded from XML
They were listed in the UI of my game (with canvas)
example:

How do I program the UI so that it works, you can help me, maybe with some examples?
Still they are not very practical new UI 4.6

using UnityEngine;
using UnityEngine.UI; // need this to use Text type
using System.Collections;
using System.Collections.Generic; // needed for using list, you might not need this one


public class TestScript : MonoBehaviour
{
    List<string> myMessages; // I need something to show


    public Transform canvasElementToPutMessagesIn;
    public Transform messageTextPrefab;

    void Start()
    {
        myMessages = new List<string>();
// impart wise message on the world... :P
        myMessages.Add("hello");
        myMessages.Add("world");
        myMessages.Add("my");
        myMessages.Add("name");
        myMessages.Add("is");
        myMessages.Add("bob");
// it isn't really :smile:

        // the bit you need to look at
        foreach(string message in myMessages)
        {
            Transform clone = Instantiate(messageTextPrefab);
            clone.SetParent(canvasElementToPutMessagesIn);
// UI elements must be parented to a canvas, or a descendant of a canvas or they wont show

            clone.GetComponent<Text>().text = message;
        }

    }
}

messageTextPrefab just needs to be a standard Text ui element (i.e. create in the hierarchy via the r-click menu and drag it into the project as a prefab), or you can style it as you would like.

if you add a “layout” component onto the UI element you target as the container for the messages you can style them as you need to.

1 Like

Thanks for the help, do experiments and tell you how it went

Hi LeflyRighty Thanks for help,
I was able to perform the function private message with your advice, but I would have another question.
As you can see from the screenshot it all works perfectly, except that does not list in the right posts.

Correct order:

<messages>
<count value="3">
<message>
<idmsg>1</idmsg>
<sender>Ashma Daeva</sender>
<title>5H/pt/8kK3wI40fwiaxP0r5IsSrE1t6fhN2tjVYSngM=</title>
<body>8/+Qod7sfRZL6eE32rl7wg==</body>
<sendlv>3</sendlv>
<readed>0</readed>
<date>2015-09-10 20:59:35</date>
</message>
<message>
<idmsg>2</idmsg>
<sender>Ashma Daeva</sender>
<title>KmZNz46Ny4CiNeCeti2zVQ==</title>
<body>h99DsUhkdI4n7zNA7WxXrA==</body>
<sendlv>3</sendlv>
<readed>0</readed>
<date>2015-09-10 20:59:31</date>
</message>
<message>
<idmsg>3</idmsg>
<sender>Ashma Daeva</sender>
<title>e8vv+nuyuGG1X5MIFbAxZw==</title>
<body>
qvVgFlKQeFK/LRVXa5CdJO50zZsw+j3whW3kEn0iyjTNo8aKoXyvXr0Pn9IzvH0szaPGiqF8r169D5/SM7x9LM2jxoqhfK9evQ+f0jO8fSzNo8aKoXyvXr0Pn9IzvH0szaPGiqF8r169D5/SM7x9LM2jxoqhfK9evQ+f0jO8fSzNo8aKoXyvXr0Pn9IzvH0srLcwkfVXHySC1YYDEcCSVJJcXUNxQCfUyFIIzEg6tzCSXF1DcUAn1MhSCMxIOrcwklxdQ3FAJ9TIUgjMSDq3MJJcXUNxQCfUyFIIzEg6tzCSXF1DcUAn1MhSCMxIOrcwp0iii5doz9+rkrfw+WjwUJT5oK2zjXEZ1fH3jVV1B6kgGLQk9sTCNTFhXUuzTr5CVw153YCzKwknkr0xf+W8v1LyIGKuF1g+S8rDuD8DFTM=
</body>
<sendlv>3</sendlv>
<readed>0</readed>
<date>2015-09-10 20:58:55</date>
</message>
</count>
</messages>

Result:


How could I improve the script so that it works well?

using UnityEngine;
using UnityEngine.UI; // need this to use Text type
using System.Collections;
using System.Collections.Generic; // needed for using list, you might not need this one


public class pm_MessageListUI : MonoBehaviour {

    public GameObject MessageUIPrefab = null;
    public Transform MessageList = null;
    public Text MessageCountText = null;
    public Text senderText = null;
    public Text titleText = null;
    public Text bodyText = null;
    public Text statusText = null;
    public Text dateText = null;


    pm_MessageClass  rdr;
    // Use this for initialization
    void Awake ()
    {
        rdr = new  pm_MessageClass("http://link"); //Not work ssl :'(
   
   
        // now display the feed items
        foreach(pm_MessageClass.messages itm in rdr.rowNews.message)
        {
            GameObject f = Instantiate(MessageUIPrefab) as GameObject;
            f.transform.SetParent(MessageList, false);
            senderText.text = itm.sender;
            titleText.text = itm.title;
            statusText.text = itm.readed;
            dateText.text = itm.date;
            // UI elements must be parented to a canvas, or a descendant of a canvas or they wont show
        }


    }
    }

This is the class:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using SecureConnection;

public class pm_MessageClass{

    XmlTextReader PMReader;
    XmlDocument PMDoc;
    XmlNode nodePM;
    XmlNode nodeMessages;
    XmlNode nodeMessage;
    public channel rowNew;

    public struct channel
    {
        public string count;

        public List<messages> message;
    }


    public struct messages
    {
        public string idmsg;
        public string sender;
        public string title;
        public string body;
        public string readed;
        public string date;
    }

    // our constructor takes the URL to the feed
    public pm_MessageClass (string feedURL){



        // setup the messages structure
        rowNew = new channel ();
        // make the list available to write to
        rowNew.message = new List<messages>();
        PMReader = new XmlTextReader (feedURL);
        PMDoc = new XmlDocument ();
        PMDoc.Load (PMReader);
        // Loop for the <messages> tag
        for (int i = 0; i < PMDoc.ChildNodes.Count; i++)
        {
            // If it is the messages tag
            if (PMDoc.ChildNodes[i].Name == "messages") {
                // <messages> tag found
                nodePM = PMDoc.ChildNodes[i];
            }
        }
        // Loop for the <count> tag
        for (int i = 0; i < nodePM.ChildNodes.Count; i++) {
            // If it is the count tag
            if (nodePM.ChildNodes[i].Name == "count") {
                // <count> tag found
                nodeMessages = nodePM.ChildNodes[i];
            }
        }


        for (int i = 0; i < nodeMessages.ChildNodes.Count; i++) {
            if (nodeMessages.ChildNodes[i].Name == "message") {
                nodeMessage = nodeMessages.ChildNodes[i];

                messages pm = new messages();
                pm.idmsg = nodeMessage["idmsg"].InnerText;
                pm.sender = nodeMessage["sender"].InnerText;
                pm.title = NetworkSecurity.Decrypt(nodeMessage["title"].InnerText);
                pm.body = NetworkSecurity.Decrypt(nodeMessage["body"].InnerText);
                pm.readed = nodeMessage["readed"].InnerText;
                pm.date = nodeMessage["date"].InnerText;

                rowNew.message.Add(pm);
            }
        }
    }
}

I wanted to use “public IEnumerator LoadMessage (string username)”
but then I would not know how to call up the list with the function Messages
StartCoroutine (NetworkManager.messages (link));
so I might as well use SSL when I am not allowed