- Home /
Reading XML Data for andriod
Hi,
I created an application and it works perfectly on my windows laptop but when I build and run it on my android device it has an issue. When I run on my android the integer and decimal data are showing as 0, while my string data is coming out fine. This is the way I am loading my data.
This is the fruitObj class containing the properties. While the fruit container is used to serialize and deserialize the fruit data from my XML File.
public class FruitObj {
[XmlAttribute("name")]
public string Name;
public int AmountPer;
public int Calories;
public decimal TotalFat; }
[XmlRoot("FruitCollection")] public class FruitContainer { [XmlArray("Fruits")] [XmlArrayItem("Fruit")] public List Fruits = new List();
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; } }
Below is my script where I initialize my fruit data to UI Text. As you can see I am loading my XML file from Script folder (found in the asset directory). UI Text variables are being initialized from the Scene
public class AppleNutritionalFacts : MonoBehaviour { bool visible; FruitObj AppleInfo; Text titleText; Text amountPerText; Text caloriesText; Text totalFatText;
void Awake()
{
visible = true;
AppleInfo = new FruitObj();
List fruitList = new List();
// var fruitCollection = FruitContainer.Load(Path.Combine(Application.dataPath,
@"Scripts\NutrionArXML.xml")); FruitContainer fc = FruitContainer.Load(Path.Combine(Application.dataPath, @"Scripts\NutrionArXML.xml")); fruitList = fc.Fruits;
foreach (FruitObj fo in fruitList)
{
if (fo.Name.ToLower().Equals("apple"))
{
AppleInfo = fo;
}
}
}
// Update is called once per frame
void Update()
{
if (AppleInfo != null)
{
string title = "<b>" + AppleInfo.Name + "
Nutritional Facts"; string amountPer = "Amount per " + > AppleInfo.AmountPer + "g"; string calories = "Calories: " + AppleInfo.Calories + "";
string totalFat = "<b>Total Fat:</b> " +
AppleInfo.TotalFat + "g\t" + AppleInfo.TotalFatPcnt + "%";
titleText.Text = title;
amountPerText.text = title;
caloriesText.Text = title;
totalFatText.Text = title;
}
} }
Do you have any idea what I am doing wrong please?
Answer by infuzo · Aug 10, 2015 at 09:00 AM
Use XMLDocument, XMLNode, XMLElement, its very simple instrument to work with XML for any system.
Your answer
Follow this Question
Related Questions
Unity/Google Play Games set up Android. Error 0 Answers
Unity5 Android mip-mapping problem 1 Answer
Parse and Unity5 for android broke? 1 Answer
Is Build and Run essential to make a Gear VR app ? 1 Answer
Artifacts while using google vr sdk 1 Answer