簡體   English   中英

將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."

我在這些鏈接中有嘗試的解決方案:將Xml反序列化為對象時出錯-預期不是xmlns ='',預期不是 xmlns =''>。 -將DeserializeXml轉換為對象時,XML文檔(2,2)中存在錯誤

但仍然無法解決我的問題。

這是我的代碼:

    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發布響應得到的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反序列化對象時,序列化程序希望根節點像類一樣被命名。 <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>

您的代碼就像一個魅力。 問題似乎不是反序列化代碼,而是服務器端的序列化代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM