I have problem when try to deserializing XML to object, I got on error message
"There is an error in XML document (2, 2)."
With innerException :
"<string xmlns='http://tempuri.org/'> was not expected."
I have try solution in these link : Error Deserializing Xml to Object - xmlns='' was not expected, xmlns=''> was not expected. - There is an error in XML document (2, 2) while DeserializeXml to object
but still not resolve my problem..
Here is my code:
bulk_response result = ConvertXMLString.convertXMLStringToObject<bulk_response>(response);
here is my Deserializing code :
public static T convertXMLStringToObject<T>(string input) where T : class
{
try
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
{
return (T)ser.Deserialize(sr);
sr.Close();
}
}
catch (Exception ex)
{
return null;
}
}
and here is my class:
public class bulk_response
{
public string status_code { get; set; }
public string status_text { get; set; }
public string transaction_id { get; set; }
}
what is the issue i couldn't find ?
Update : This is the xml i get from http post response:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/"><?xml version="1.0" encoding="utf-16"?>
<bulk_response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<status_code>hansen</status_code>
<status_text>angie</status_text>
<transaction_id>ini testing aja</transaction_id>
</bulk_response></string>
and this is how i passing data via http post and get the response :
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(destinationUrl);
// add the parameters as key valued pairs making
// sure they are URL encoded where needed
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postData = encoding.GetBytes(param);
httpReq.ContentType = "application/x-www-form-urlencoded";
httpReq.Method = "POST";
httpReq.ContentLength = postData.Length;
// convert the request to a steeam object and send it on its way
Stream ReqStrm = httpReq.GetRequestStream();
ReqStrm.Write(postData, 0, postData.Length);
ReqStrm.Close();
// get the response from the web server and
// read it all back into a string variable
HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
StreamReader respStrm = new StreamReader(
httpResp.GetResponseStream());
string result = respStrm.ReadToEnd();
httpResp.Close();
respStrm.Close();
return result;
How do you serialize your XML? It looks pretty messed up.
<?xml ...
after the <string>
tag looks quite strange to me. I have never seen this. Is this valid XML?<string> was not expected.
means - it'd expect a bulk_response
-tag insteadstatus_text
is no closing-tag, should be <status_text>angie</status_text>
xmlns
definitions on different levels is also uncommon (if it's legal XML at all) - but I do not see why you need them at all, you could just leave themHaving said that, after simplifying your XML to
<?xml version="1.0" encoding="utf-8"?>
<bulk_response>
<status_code>hansen</status_code>
<status_text>angie</status_text>
<transaction_id>ini testing aja</transaction_id>
</bulk_response>
your code works like a charm. The issue does not seem to be the deserializing code, but the serializing code on the server side.