- Home /
xml loading in unity andriod
Hi I created an XML file containing the data of 3 fruits. I imported the XML file in Assets/Scripts folder now I am trying to connect to it and retrieve the data using the following code. I followed this example http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer
public class FruitObj : MonoBehaviour {
[XmlAttribute("name")]
public string Name;
public int Sugar;
public decimal Protein;
}
[XmlRoot("FruitCollection")]
public class FruitContainer
{
[XmlArray("Fruits")]
[XmlArrayItem("Fruit")]
public List<FruitObj> Monsters = new List<FruitObj>();
public static FruitContainer Load(string path)
{
var serializer = new XmlSerializer(typeof(FruitContainer));
using (var stream = new FileStream(path, FileMode.Open))
{
return serializer.Deserialize(stream) as FruitContainer;
}
}
//Loads the xml directly from the given string. Useful in combination with www.text.
public static FruitContainer LoadFromText(string text)
{
var serializer = new XmlSerializer(typeof(FruitContainer));
return serializer.Deserialize(new StringReader(text)) as FruitContainer;
}
}
and I am trying to connect in my gameobject using
public class AppleNutritionalFacts : MonoBehaviour
{
void Start()
{
var fruitCollection = FruitContainer.Load(Path.Combine(Application.streamingAssetsPath, "NutrionArXML.xml"));
}
}
Then I am getting this error
Can anyone please tell me what im doing wrong?
FruitObj should not inherits from $$anonymous$$onoBehavior, sorry.
I'm unfamiliar with this method of reading X$$anonymous$$L in Unity unfortunately. But if you are continuing to hit a roadblock with your current approach (don't give up yet!!), there are alternative methods of importing X$$anonymous$$L. I've included a couple of links that were helpful for me. They're beginner level, but you can easily build them up and tailor them to your project. Just in case!
https://web.archive.org/web/20140702084233/http://unitygems.com/xml/ http://unitynoobs.blogspot.co.uk/2011/02/xml-loading-data-from-xml-file.html http://blog.sokay.net/2012/08/14/loading-xml-in-unity-building-a-stage-from-xml/