将 Xml 反序列化为对象时出错 - {<string xmlns='http://tempuri.org/'> 不是预期的。}

Error Deserializing Xml to Object - {<string xmlns='http://tempuri.org/'> was not expected.}

我在尝试将 XML 反序列化为对象时遇到问题, 我收到错误消息

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

有 innerException :

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

我尝试了这些 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

但还是没有解决我的问题..

这是我的代码:

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

这是我的 class:

  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>

这就是我通过 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;

如何序列化您的 XML?看起来很乱。

  1. <string> 标签后面的附加 <?xml ... 我觉得很奇怪。我从没见过这个。这是否有效 XML?
  2. 当您从 XML 反序列化一个对象时,序列化程序期望根节点的命名类似于 class。这就是 <string> was not expected. 的意思——它期望 bulk_response-tag 而不是
  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>

你的代码很有魅力。问题似乎不是反序列化代码,而是服务器端的序列化代码。