Hello everyone,
Our project has to connect to a web page in order to extract the content of an XML file located on that web page, and converting that result into a profile for our players. The connection and extraction process itself works nicely ; I’ve managed to extract a string containing the xml with that piece of code :
public string SendLoginUrl = "the url referencing the xml file"
public IEnumerator SendLoginCo (string firstName, string lastName, string birthdate)
{
//Debug.Log ("SEND LOGIN CO");
if (Application.internetReachability != NetworkReachability.NotReachable) {
WWWForm post = new WWWForm ();
post.AddField ("firstName", firstName);
post.AddField ("lastName", lastName);
post.AddField ("birthdate", birthdate);
using (UnityWebRequest www = UnityWebRequest.Post(SendLoginUrl, post))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
if (CompteurErrorLogin < 3)
{
CompteurErrorLogin++;
StartCoroutine("ReSendLoginCo", post);
}
}
else
{
//transmission done
Debug.Log("Transmission data done");
ResponseTraitement(www.downloadHandler.text);
}
}
}
else {
Debug.Log ("Votre ordinateur n'est pas connecté");
}
}
Now I have to check if that string contains an error, and if it doesn’t, send it to the deserialization method :
public void ResponseTraitement (string text)
{
XmlDocument document = new XmlDocument ();
document.LoadXml (text);
Debug.Log (text);
if (document.DocumentElement.Name == "error") {
string code = document.DocumentElement ["code"].InnerText;
string message = document.DocumentElement ["message"].InnerText;
Debug.Log (code + " " + message);
}
else
{
CurrentProfil = Profil.LoadFromText (text);
UpdateData ();
}
}
And convert it into a class of type “Profil” that looks like this :
//[System.Serializable]
[XmlRoot("userData")]
public class Profil {
public int uid;
[XmlIgnore]
public string firstName;
[XmlElement("firstName")]
public XmlCDataSection firstNameCDATA
{
get
{
XmlDocument doc = new XmlDocument();
return doc.CreateCDataSection(firstName);
}
set
{
firstName = value.Value;
}
}
[XmlIgnore]
public string lastName;
[XmlElement("lastName")]
public XmlCDataSection lastNameCDATA
{
get
{
XmlDocument doc = new XmlDocument();
return doc.CreateCDataSection(lastName);
}
set
{
lastName = value.Value;
}
}
//[XmlIgnore]
//public string birthdate;
//[XmlElement("birthdate")]
//public XmlCDataSection birthdateCDATA
//{
// get
// {
// XmlDocument doc = new XmlDocument();
// return doc.CreateCDataSection(birthdate);
// }
// set
// {
// birthdate = value.Value;
// }
//}
[XmlArray("checkpoints"), XmlArrayItem("checkpoint")]
public List<SavedCheckpoint> checkpointsList = new List<SavedCheckpoint>();
[XmlArray("images"), XmlArrayItem("image")]
public List<SavedScreenshot> screenshostList = new List<SavedScreenshot>();
[XmlIgnore]
public string avatar;
[XmlElement("avatar")]
public XmlCDataSection avatarCDATA
{
get
{
XmlDocument doc = new XmlDocument();
return doc.CreateCDataSection(avatar);
}
set
{
if (value != null)
{
avatar = value.Value;
}
}
}
public int isNew;
public Profil()
{
}
public static Profil LoadFromText(string text)
{
//This method returns an InvalidCastException :
XmlSerializer serializer = new XmlSerializer(typeof(Profil));
Profil p;
StringReader reader = new StringReader(text);
Debug.Log("READER : " + reader.ReadToEnd());
p = (Profil)serializer.Deserialize(reader);
reader.Close();
return p;
}
//public string avatar;
}
the last function of the “Profil” class, LoadFromText(), returns an InvalidCastException each time it reaches the Deserialize() method :
System.InvalidOperationException: There is an error in XML document (2, 196). ---> System.InvalidCastException: Specified cast is not valid.
What I want to know is, does the error comes from the string “text”, or from Deserialize ? i’ve double checked the format of the string, and I’ve tried the serialization - deserialization process on a different project with different classes to see if the class itself was the root of the problem, but no. Any idea ?