Note that there are some explanatory texts on larger screens.

plurals
  1. POXML Deserialization with Servicestack.Text
    primarykey
    data
    text
    <p>I am learning <a href="https://github.com/ServiceStack/ServiceStack.Text" rel="nofollow noreferrer">Servicestack.Text</a> Library as it has some of the best features.I am trying to deserialize XML into one of my DTOs as below;</p> <p><strong>C# Code:</strong>[Relevant Code with Console Application Here]</p> <pre><code>class Program { static void Main(string[] args) { string str = "http://static.cricinfo.com/rss/livescores.xml"; WebClient w = new WebClient(); string xml = w.DownloadString(str); Response rss = xml.FromXml&lt;Response&gt;(); foreach (var item in rss.rss.Channel.item) { Console.WriteLine(item.title); } Console.Read(); } } </code></pre> <p>You can go through the XML file at <code>str</code>[Given in the program]. I have prepared DTOs for the deserialization. They are as below:</p> <pre><code>public class Response { public RSS rss { get; set; } } public class RSS { public string Version { get; set; } public ChannelClass Channel { get; set; } } public class ChannelClass { public string title { get; set; } public string ttl { get; set; } public string description { get; set; } public string link { get; set; } public string copyright { get; set; } public string language { get; set; } public string pubDate { get; set; } public List&lt;ItemClass&gt; item { get; set; } } public class ItemClass { public string title { get; set; } public string link { get; set; } public string description { get; set; } public string guid { get; set; } } </code></pre> <p>When I run the program, I get an exception as shown below:</p> <p><img src="https://i.stack.imgur.com/lUEXA.png" alt="enter image description here"></p> <p>So, to change the <code>Element</code> and the <code>namespace</code>, I did following workaround:</p> <p>I put the <code>DataContractAttribute</code> on my <code>Response</code> class as below:</p> <pre><code>[DataContract(Namespace = "")] public class Response { public RSS rss { get; set; } } </code></pre> <p>I changed the <code>Element</code> name as below by adding following two lines just before deserializing</p> <pre><code> //To change rss Element to Response as in Exception xml = xml.Replace("&lt;rss version=\"2.0\"&gt;","&lt;Response&gt;"); //For closing tag xml = xml.Replace("&lt;/rss&gt;","&lt;/Response&gt;"); </code></pre> <p>But, it gave another exception on the foreach loop as the deserialized <code>rss</code> object was <code>null</code>. So, how should I deserialize it in a proper way using <code>Servicestack.Text</code>?</p> <p><strong>Note :</strong> </p> <p>I know well how to deserialize with other libraries, I want to do it with ServiceStack only.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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