Note that there are some explanatory texts on larger screens.

plurals
  1. POC# XML Deserialize xmlns
    text
    copied!<p>My code:</p> <pre><code>CampaignList myObject; XmlSerializer mySerializer = new XmlSerializer(typeof(CampaignList)); **myObject = (CampaignList)mySerializer.Deserialize(xmlDoc.CreateReader());** </code></pre> <p>Error:</p> <pre><code>&lt;CampaignListXml xmlns='api.paycento.com/1.0'&gt; werd niet verwacht. [InvalidOperationException: &lt;CampaignListXml xmlns='api.paycento.com/1.0'&gt; werd niet verwacht.] Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCampaignList.Read4_CampaignListXml() +214 </code></pre> <p>The xml response:</p> <pre><code>&lt;CampaignListXml xmlns="api.paycento.com/1.0" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;Allcampaign&gt; &lt;/Allcampaign&gt; &lt;/CampaignListXml&gt; </code></pre> <p>I've tried adding the encoding parameter to the Deserialize method, but it gives an error "UTF8 not supported for XMLSerializer". I tried UTF8, UTF-8 and System.Text.Encoding.UTF8.EncodingName;</p> <p>Here is the entire code, if you want to follow it.</p> <pre><code> public IEnumerable&lt;Campaign&gt; GetCampaigns() { return GetCampaigns("active", 0, 50, "", ""); } public IEnumerable&lt;Campaign&gt; GetCampaigns(string status, int startIndex, int pageSize, string orderby, string sort) { return GetCampaigns(status, startIndex, pageSize, orderby, sort, SessionKey); } public IEnumerable&lt;Campaign&gt; 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}&amp;start={1}&amp;psize={2}&amp;orderby={3}&amp;sort={4}&amp;session={5}", status, startIndex, pageSize, orderby, sort, sessionKey); string uri = BASE_URL + suffix; string response = PerformAndReadHttpRequest(uri, "GET", ""); string xml = "&lt;CampaignListXml " + response.Substring(response.IndexOf('&gt;')); 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")) &amp;&amp; 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)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload