- Home /
create string array from xml
i need to create an array of strings from an xml file on a server:
IEnumerator Start(){
string XMLPareti = "http://www.sblinko.com/store/index.php?controller=categories";
WWW www = new WWW(XMLPareti);
//Load the data and yield (wait) till it's ready before we continue executing the rest of this method.
yield return www;
if (www.error == null){
//Sucessfully loaded the XML
Debug.Log("Loaded following XML " + www.data);
//Create a new XML document out of the loaded data
//XmlDocument xmlDoc = new XmlDocument();
// xmlDoc.LoadXml(www.data);
XElement paretiXml = XElement.Load(XMLPareti);
IEnumerable<string> llinks = from pLink in paretiXml.Elements ("category")
let urls = (string)pLink.Element("link")
orderby urls
select urls;
Links = new ArrayList();
foreach (string lk in llinks){
Debug.Log(lk);
Links.Add (lk);
String[] paretiToLoad = (String[]) Links.ToArray( typeof( string ));
ParetiToLoad = paretiToLoad;
Debug.Log(ParetiToLoad.Length);
}
}else{
Debug.Log("ERROR: " + www.error);
}
}
The problem is when i try to use the data from the created array ParetiToLoad, i can see only one item but in console show me all needed nodes. How can i add all the nodes "link" inside the string array? Thanks in advance for any help!!
Out of curiosity and to better understand the task, can I ask what the purpose of converting the xml into an array of strings is?
I'm creating a store and I have to populate the gui, descriptions, images, etc., with an xml file. Create the array with the data taken from the xml it seemed the most practical way to use the data, but I am open to any advice.. i'm new in the use of X$$anonymous$$L..
There are a number of ways dependent on the need and comfort. You could use xpath to navigate but this can sometimes be clunky. Personally I would use LINQ, basically it allows you to iterate and navigate through xml as if you were using objects.
heres a quick example
http://www.dotnetcurry.com/ShowArticle.aspx?ID=564
and heres the reference
http://msdn.microsoft.com/en-us/library/system.xml.linq.aspx
Let me know if that helps, if not I can help you do it the traditional way. Even so it might be worth writing a class for the nodes and attributes .. especially if theres a generic template you can create one from :)
Thanks a lot, linq seems to be easier. Have any limitations? What are the advantages of the traditional way? Thanks for the help
Answer by whydoidoit · Aug 19, 2013 at 08:59 PM
I'd suggest scripting classes to map to your data structure and then using XmlSerializer to recreate an object graph:
[XmlRoot("rootTag")]
public class SomeClass
{
[XmlAttribute] public string anAttribute;
[XmlElement] public int anElement;
[XmlArray][XmlArrayItem("thing")] Thing things;
}
[XmlRoot("thing")]
public class Thing
{
[XmlElement] public string element;
[XmlAttribute] public int integer;
}
Which supports Xml formatted as:
<rootTag anAttribute="This is me">
<anElement>1</anElement>
<things>
<thing integer = "1">
<element>Hello</element>
</thing>
<thing integer = "2">
<element>World</element>
</thing>
</things>
</rootTag>
Then you could use this utility code:
using UnityEngine;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System;
public static class XmlSupport
{
public static T DeserializeXml<T> (this string xml) where T : class
{
if( xml != null ) {
var s = new XmlSerializer (typeof(T));
using (var m = new MemoryStream (Encoding.UTF8.GetBytes (xml)))
{
return (T)s.Deserialize (m);
}
}
return null;
}
public static object DeserializeXml(this string xml, Type tp)
{
var s = new XmlSerializer (tp);
using (var m = new MemoryStream (Encoding.UTF8.GetBytes (xml)))
{
return s.Deserialize (m);
}
}
public static string SerializeXml (this object item)
{
var s = new XmlSerializer (item.GetType ());
using (var m = new MemoryStream())
{
s.Serialize (m, item);
m.Flush ();
return Encoding.UTF8.GetString (m.GetBuffer ());
}
}
}
And to get a SomeClass do this:
var instance = someStringOfXml.Deserialize<SomeClass>();
Debug.Log(string.Format("{0} has {1} things", instance.anAttribute, instance.things.Length));
See these references:
You can of course use Linq on the objects returned by this method.
I'm trying your suggestion. when i call someclass, what is someStringOfXml? I haven't nothing that contains definition for Deserialize. Thanks for the help
Answer by Eclipsed · Aug 09, 2013 at 01:01 PM
Well, on line 22 you're overwriting the "ParetiToLoad" array for each entry in the xml.
This looks like an error, but I should check the xml structure to fully understand what you're trying to do ;)
Cheers.
Your answer
Follow this Question
Related Questions
Trouble updating old Unity syntax 0 Answers
C#-XMLSerialize a Struct with Vector3 Array in it 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can i play with text which have string and int (from XML) 2 Answers