简体   繁体   English

将Xml反序列化为对象时出错-{ <string xmlns='http://tempuri.org/'> 没想到。}

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

I have problem when try to deserializing XML to object, I got on error message 尝试反序列化XML到对象时遇到问题,出现错误消息

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

With innerException : 随着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. 我在这些链接中有尝试的解决方案:将Xml反序列化为对象时出错-预期不是xmlns ='',预期不是 xmlns =''>。 - There is an error in XML document (2, 2) while DeserializeXml to object -将DeserializeXml转换为对象时,XML文档(2,2)中存在错误

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: 更新:这是我从http发布响应得到的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>

and this is how i passing data via http post and get the response : 这就是我通过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;

How do you serialize your XML? 您如何序列化XML? It looks pretty messed up. 看起来很乱。

  1. The additional <?xml ... after the <string> tag looks quite strange to me. <string>标记之后的附加<?xml ...对我来说似乎很奇怪。 I have never seen this. 我从未见过。 Is this valid XML? 这是有效的XML吗?
  2. When you deserialze an object from XML, the serializer expectes the root node to be named like the class. 当您从XML反序列化对象时,序列化程序希望根节点像类一样被命名。 This is what <string> was not expected. <string> was not expected. means - it'd expect a bulk_response -tag instead 意味着-它期望使用bulk_response -tag代替
  3. The closing-tag of status_text is no closing-tag, should be <status_text>angie</status_text> status_text的结束标记不是结束标记,应为<status_text>angie</status_text>
  4. Having 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 them 在不同级别上具有xmlns定义也是不常见的(如果完全是合法的XML)-但我不明白为什么根本需要它们,您可以只留下它们

Having said that, after simplifying your XML to 话虽如此,将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>

your code works like a charm. 您的代码就像一个魅力。 The issue does not seem to be the deserializing code, but the serializing code on the server side. 问题似乎不是反序列化代码,而是服务器端的序列化代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何隐藏名称空间 <string xmlns=“http://tempuri.org/”> 在网络服务? - How to hide the namespace <string xmlns=“http://tempuri.org/”> in webservice? {”<user xmlns=''> 不是预期的。} 反序列化 Twitter XML - {"<user xmlns=''> was not expected.} Deserializing Twitter XML 尝试序列化参数http://tempuri.org/:callback时发生错误 - There was an error while trying to serialize parameter http://tempuri.org/:callback Web服务中的http://tempuri.org/? - http //tempuri.org/ in Web services? 将 Xml 反序列化为对象时出错 - 不需要 xmlns=&#39;&#39; - Error Deserializing Xml to Object - xmlns='' was not expected xmlns=&#39;&#39;&gt; 不是预期的。 - DeserializeXml to object 时 XML 文档 (2, 2) 中存在错误 - xmlns=''> was not expected. - There is an error in XML document (2, 2) while DeserializeXml to object xmlns=&#39;&#39;&gt; 不是预期的。 - XML 文档中存在错误 (2, 2) - xmlns=''> was not expected. - There is an error in XML document (2, 2) 在某些地方使用了[WebService(Namespace =“ http://tempuri.org/”)]? - does [WebService(Namespace = “http://tempuri.org/”)] is used some where? 尝试序列化参数http://tempuri.org/:exception时发生错误。 在C#中 - There was an error while trying to serialize parameter http://tempuri.org/:exception. in C# C#从xml反序列化到对象( <result xmlns=''> 没想到。) - C# Deserilize from xml to object (<result xmlns=''> was not expected.)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM