2017-08-14 236 views
0

我有问题时尝试反序列化XML到对象, 我上的错误消息错误反序列化XML来对象 - {<字符串的xmlns =“HTTP://tempuri.org/”>是没有预料到。}

"There is an error in XML document (2, 2)." 

随着的InnerException:

"<string xmlns='http://tempuri.org/'> was not expected." 

我对这些链接尝试的解决方案: Error Deserializing Xml to Object - xmlns='' was not expectedxmlns=''> was not expected. - There is an error in XML document (2, 2) while DeserializeXml to object

,但仍然不属于R esolve我的问题..

这里是我的代码:

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; } 
    } 

什么问题我找不到?

更新: 这是XML我从HTTP POST响应得到:

<?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> 

,这是我如何通过HTTP POST数据传递和获取响应:

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; 
+2

你的XML文档是什么样的? –

+0

这是我从http post response得到的xml:<?xml version =“1.0”encoding =“utf-8”?> <?xml version = “1.0”encoding =“utf-16”?> 汉森 安吉 INI测试AJA </TRANSACTION_ID> Hans

+1

请不要在评论中添加细节,但**编辑* *您的问题 –

回答

1

你如何序列化你的XML?它看起来相当混乱。

  1. 额外的<?xml ...<string>标签看起来很奇怪。我从来没有见过这个。这是有效的XML吗?
  2. 当您从XML中反序列化一个对象时,序列化程序会将根节点命名为类的名称。这是什么意思<string> was not expected. - 它会期望一个bulk_response - 标签代替
  3. status_text的关闭标签没有结束标签,应该是<status_text>angie</status_text>
  4. 有不同层次的xmlns定义也是罕见(如果它是合法的XML在所有) - 但我不明白为什么你需要他们在所有的,你可以只让他们

话虽如此,简化您的XML

<?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> 

你的代码后的作品像一个魅力。这个问题似乎不是反序列化代码,而是服务器端的序列化代码。

+0

关闭标签,这是我的错字,当我更新我的问题..嗯,我看到我看到... – Hans

+0

我的代码,当我得到的HTTP后回应是正确的? – Hans

相关问题