I was able to find a solid solution at this link
I put the static method in my server
public static string GetXMLFromObject(object o)
{
StringWriter sw = new StringWriter();
XmlTextWriter tw = null;
try
{
XmlSerializer serializer = new XmlSerializer(o.GetType());
tw = new XmlTextWriter(sw);
serializer.Serialize(tw, o);
}
catch (Exception ex)
{
//Handle Exception Code
}
finally
{
sw.Close();
if (tw != null)
{
tw.Close();
}
}
return sw.ToString();
}
And all worked well.
And then the method for sending the response called the function like in the link
[HttpGet]
public string GetData(){
Employee emp = new Employee();
emp.FirstName = "Code";
emp.LastName = "Handbook";
string xml = GetXMLFromObject(emp);
return xml;
}
And in my Unity C# script, I parsed the response using the second static method given in the link but two changes had to be made.
public static Object ObjectToXML(string xml, Type objectType)
{
StringReader strReader = null;
XmlSerializer serializer = null;
XmlTextReader xmlReader = null;
Object obj = null;
try
{
strReader = new StringReader(xml);
serializer = new XmlSerializer(objectType);
xmlReader = new XmlTextReader(strReader);
obj = serializer.Deserialize(xmlReader);
}
catch (Exception exp)
{
//Handle Exception Code
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
if (strReader != null)
{
strReader.Close();
}
}
return obj;
}
The changes were easy enough to implement, they were:
Unity C# does not support XmlReader using the System namespace so I had to add a conditional statement to the using statement. You can read more about it in the official unity documentation here
#if NETFX_CORE
using XmlReader = WinRTLegacy.Xml.XmlReader;
#else
using XmlReader = System.Xml.XmlReader;
#endif
The second change was that strReader.Close() does not exist in the Unity C# scripting.
Change this:
if (strReader != null)
{
strReader.Close();
}
To this:
if (strReader != null)
{
strReader.Dispose();
}
I found that solution in a forum here
One last thing to note, Object was giving me some issues so I had to add the System to it. System.Object although Visual Studio said I did not need it, errors were thrown with out it. The final method looked like this:
public static System.Object ObjectToXML(string xml, Type objectType)
{
StringReader strReader = null;
XmlSerializer serializer = null;
XmlTextReader xmlReader = null;
System.Object obj = null;
try
{
strReader = new StringReader(xml);
serializer = new XmlSerializer(objectType);
xmlReader = new XmlTextReader(strReader);
obj = serializer.Deserialize(xmlReader);
}
catch (Exception exp)
{
//Handle Exception Code
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
if (strReader != null)
{
strReader.Dispose();
}
}
return obj;
}
And to use it, I did this:
// handled response above
string xml = response.text;
Employee emp = new Employee();
try{
System.Object obj = ObjectToXML(xml,typeof(Employee));
emp = (Employee)obj;
}
catch (Exception ex){
Debug.Log(ex.message);
emp = null;
}
finally{
if(emp != null){
// do stuff
}
else{
// handle error
}
}
This was a lot easier than trying to parse JSON and I am able to throw the XML right into an object. The actual object I used was more complex than the Employee example and it worked just fine.