我在尝试将XML反序列化为对象时遇到问题,
我收到错误消息
"There is an error in XML document (2, 2)."
内部异常:
"<string xmlns='http://tempuri.org/'> was not expected."
我在以下链接中尝试了解决方案:
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
bulk_response result = ConvertXMLString.convertXMLStringToObject<bulk_response>(response);
以下是我的反序列化代码:
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;
}
}
这是我的课:
public class bulk_response
{
public string status_code { get; set; }
public string status_text { get; set; }
public string transaction_id { get; set; }
}
我找不到什么问题?
这是我从http post响应中获得的xml:
<?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>
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(destinationUrl);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postData = encoding.GetBytes(param);
httpReq.ContentType = "application/x-www-form-urlencoded";
httpReq.Method = "POST";
httpReq.ContentLength = postData.Length;
Stream ReqStrm = httpReq.GetRequestStream();
ReqStrm.Write(postData, 0, postData.Length);
ReqStrm.Close();
HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
StreamReader respStrm = new StreamReader(
httpResp.GetResponseStream());
string result = respStrm.ReadToEnd();
httpResp.Close();
respStrm.Close();
return result;