- Home /
XML into Unity Class - Array and Attributes
Hello, I cannot seem to get my head around on how to deserialise this XML into a class for unity. I've been using the Xml.Serialization, but I cannot seem to get the Attributes correct.
XML;
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RunSourceResponse xmlns="http://tempuri.org/">
<RunSourceResult>
<Results>
<Results>
<ProductCode>0022KN</ProductCode>
<ProductName>ABC</ProductName>
<ProductAvailability>Intermediaries Only</ProductAvailability>
<ApplyOnline>false</ApplyOnline>
<LenderCode>XYZ</LenderCode>
<LenderName>ABC</LenderName>
</Results>
<Results>
<ProductCode>0022KN</ProductCode>
<ProductName>ABC</ProductName>
<ProductAvailability>Intermediaries Only</ProductAvailability>
<ApplyOnline>false</ApplyOnline>
<LenderCode>XYZ</LenderCode>
<LenderName>ABC</LenderName>
</Results>
<Results>
<ProductCode>0022KN</ProductCode>
<ProductName>ABC</ProductName>
<ProductAvailability>Intermediaries Only</ProductAvailability>
<ApplyOnline>false</ApplyOnline>
<LenderCode>XYZ</LenderCode>
<LenderName>ABC</LenderName>
</Results>
<Results>
<ProductCode>0022KN</ProductCode>
<ProductName>ABC</ProductName>
<ProductAvailability>Intermediaries Only</ProductAvailability>
<ApplyOnline>false</ApplyOnline>
<LenderCode>XYZ</LenderCode>
<LenderName>ABC</LenderName>
</Results>
</Results>
</RunSourceResult>
</RunSourceResponse>
</s:Body>
</s:Envelope>
This is coming externally from Unity, from a server, and this is how I've made the class, but I'm not getting any results;
[System.Serializable]
[XmlRoot("s:Envelope")]
public class Envelope {
[System.Serializable]
[XmlRoot("s:Body")]
public class Body {
[System.Serializable]
[XmlRoot("RunSourceResponse")]
public class RunSourceResponse {
[System.Serializable]
[XmlRoot("RunSourceResult")]
public class RunSourceResult {
[System.Serializable]
[XmlRoot("Results")]
public class Results {
[System.Serializable]
//[XmlArray("Results")]
public class Result {
public string ProductCode;
}
}
}
}
}
}
UPDATE
I've updated my structs to this, but still with no luck
[XmlRoot("s:Envelope")]
[System.Serializable]
public struct Envelope {
[XmlAttribute("s:Body")]
public Body body;
}
[XmlRoot("s:Body")]
[System.Serializable]
public struct Body {
[XmlAttribute("RunSourceResponse")]
public RunSourceResponse runSourceResponse;
}
[XmlRoot("RunSourceResponse")]
[System.Serializable]
public struct RunSourceResponse {
[XmlAttribute("RunSourceResult")]
public RunSourceResult runSourceResult;
}
[XmlRoot("RunSourceResult")]
[System.Serializable]
public struct RunSourceResult {
[XmlAttribute("Results")]
public Results results;
}
[XmlRoot("Results")]
[System.Serializable]
public struct Results {
[XmlElement("Results")]
public Result[] _result;
}
public Envelope _results;
[System.Serializable]
public struct Result {
public string ProductCode;
public string ProductName;
public string ProductAvailability;
public bool ApplyOnline;
}
This is how you can map main 'Results' array:
[XmlElement("Results")] public Results[] results_array;
Also remember to flag Results struct (and every other involved) as [System.Serializable].
Ok, I noticed this sample code just now. So... the thing is - this is not how one mirror xml in c#. Ie. you don't express structure by nesting those types but creating just fields of those types.
Check this: Wolfram Alpha Xml deserialization sample
hello, thanks for the help - I've updated my question as I'm still not getting results. I get an error, cannot serialise member 'body'.
One additional thing, in xml like here:
<s:Envelope ...>
"s:" is not part of the name but some kind of attribute (i think), so remove it from your [XmlRoot("s:Envelope")] and [XmlRoot("s:Body")]
Also, paste your xml to online xml2c# converters for quick and dirty first drafts
Your answer
Follow this Question
Related Questions
xmlserializer Serializing/deserializing error 1 Answer
XML Error on Load 1 Answer
Best Way to Store Large Number of GameObjects? 1 Answer
How to type Color32 format into XML? 1 Answer
does xml serializer good to store images and 3d models 0 Answers