HI ,
I am trying to create a data class from a xml file . it’s work great on element and it give me it;s information ( attribute , … ) but when it comes to element that is implemented by another class and has a instance in class … it’s return NULL …
XML_Importer :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
public class XML_Importer
{
public static object DeserializeXML (string filePath, System.Type type)
{
XmlSerializer deserializer = new XmlSerializer(type);
TextReader reader = new StreamReader(filePath);
object instance = deserializer.Deserialize(reader);
reader.Close();
return instance;
}
}
XML File :
<?xml version="1.0" encoding="utf-8"?>
<Sadaf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Objects>
<Object Size="" Id="3" Name="Obj1" DefaultCamera="1" Model="File.model">
<Position Type="Relative" X="" Y="" Z="">
<RelatedTo Id="6"></RelatedTo>
</Position>
</Object>
</Objects>
</Sadaf>
DataClass :
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
[XmlRoot("Sadaf")]
public class Sadaf
{
[XmlArray("Objects"), XmlArrayItem("Object", typeof(Object))]
public Object[] Objects;
public class Object
{
[XmlAttribute("Id")]
public int id { get; set; }
[XmlAttribute("Name")]
public string name { get; set; }
[XmlAttribute("DefaultCamera")]
public int defualtCamera { get; set; }
[XmlAttribute("Model")]
public string model { get; set; }
[XmlElement("MetaData")]
public MetaData metaData;
//[XmlElement("Position")]
public Position position;
}
public class Position
{
[XmlAttribute("Type")]
public string type { get; set; }
[XmlAttribute("X")]
public int x { get; set; }
[XmlAttribute("Y")]
public int y { get; set; }
[XmlAttribute("Z")]
public int z { get; set; }
public RelatedTo relatedTo { get; set; }
}
public class RelatedTo
{
[XmlAttribute("Id")]
public int id { get; set; }
}
}
DataClass temp = (DataClass)XML_Importer.DeserializeXML("test_XML.xml", typeof(DataClass));
for (int i = 0 ; i < temp.Objects.Length ; i++)
{
Debug.Log(temp.Objects[i].id);
Debug.Log(temp.Objects[i].position.type);
}
It;s got null reference error on " Debug.Log(temp.Objects[i].position.type); " Line …
What do you folks think is the problem ??
Thanks.