2

我的代码:

CampaignList myObject;
XmlSerializer mySerializer = new XmlSerializer(typeof(CampaignList));
**myObject = (CampaignList)mySerializer.Deserialize(xmlDoc.CreateReader());**

错误:

<CampaignListXml xmlns='api.paycento.com/1.0'> werd niet verwacht.
 [InvalidOperationException: <CampaignListXml
 xmlns='api.paycento.com/1.0'> werd niet verwacht.]   
 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCampaignList.Read4_CampaignListXml()
 +214

xml响应:

<CampaignListXml xmlns="api.paycento.com/1.0" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Allcampaign>
</Allcampaign>
</CampaignListXml>

我尝试将编码参数添加到 Deserialize 方法,但它给出了错误“XMLSerializer 不支持 UTF8”。我尝试了 UTF8、UTF-8 和 System.Text.Encoding.UTF8.EncodingName;

这是整个代码,如果您想遵循它。

    public IEnumerable<Campaign> GetCampaigns()
    {
        return GetCampaigns("active", 0, 50, "", "");
    }
    public IEnumerable<Campaign> GetCampaigns(string status, int startIndex, int pageSize, string orderby, string sort)
    {
        return GetCampaigns(status, startIndex, pageSize, orderby, sort, SessionKey);
    }
    public IEnumerable<Campaign> GetCampaigns(string status, int startIndex, int pageSize, string orderby, string sort, string sessionKey)
    {
        if (string.IsNullOrEmpty(sessionKey) || sessionKey.Length != 34)
            throw new ArgumentException("Session key must be 34 chars long. " + sessionKey.Length);
        string suffix = String.Format("campaigns/all/?status={0}&start={1}&psize={2}&orderby={3}&sort={4}&session={5}", status, startIndex, pageSize, orderby, sort, sessionKey);
        string uri = BASE_URL + suffix;
        string response = PerformAndReadHttpRequest(uri, "GET", "");
        string xml = "<CampaignListXml " + response.Substring(response.IndexOf('>'));      
        CampaignList myObject;
          XmlReaderSettings settings = new XmlReaderSettings();
          using (StringReader textReader = new StringReader(xml))
          {
              using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
              {
                  XmlSerializer mySerializer = new XmlSerializer(typeof(CampaignList));
                  myObject = (CampaignList)mySerializer.Deserialize(xmlReader);
              }
          }
        return myObject.Campaign;
    }
    #endregion
    #region util methods
    private HttpWebResponse DoHttpWebRequest(String uri, String method, string data)
    {
        HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
        req.KeepAlive = false;
        req.ContentType = "application/xml";
        req.Method = method;
        if ((method.Equals("POST") || method.Equals("PUT")) && data != null)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(data);
            Stream PostData = req.GetRequestStream();
            PostData.Write(buffer, 0, buffer.Length);
            PostData.Close();
        }
        return req.GetResponse() as HttpWebResponse;
    }
    private string ReadHttpResponse(HttpWebResponse response)
    {
        Encoding enc = System.Text.Encoding.UTF8;
        StreamReader loResponseStream = new StreamReader(response.GetResponseStream(), enc);
        string returnVal = loResponseStream.ReadToEnd();
        loResponseStream.Close();
        response.Close();
        return returnVal;
    }
    private string PerformAndReadHttpRequest(String uri, String method, string data)
    {
        return ReadHttpResponse(DoHttpWebRequest(uri, method, data));
    }
4

2 回答 2

5

您的CompaignList类应该用 XmlRootAttribute 装饰,如下所示

[XmlRoot("CampaignListXml", Namespace = "api.paycento.com/1.0")]
public class CampaignList
{
}

然后你可以从xml反序列化它,如下所示

using (StringReader reader = new StringReader(xml))
{
    XmlSerializer serializer = new XmlSerializer(typeof(CampaignList));
    CampaignList x1 = serializer.Deserialize(reader) as CampaignList;
}
于 2012-04-17T20:17:45.547 回答
0

用这个:

    XmlReaderSettings settings = new XmlReaderSettings();
    using (StringReader textReader = new StringReader(xml))
    {
       using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
       {
         myObject =(CampaingList)mySerializer.Deserialize(xmlReader);
       }
    }

这样,您可以使用中间文本阅读器直接反序列化 xml 字符串。

当您不再需要它们时,这些使用可以关闭/处理流。如果你觉得更舒服,你可以明确地重写它。

于 2012-04-17T12:52:14.503 回答