Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deserialising does not work

Tags:

c#

xml

This is the xml stream:

<?xml version="1.0" encoding="utf-8" ?> 
<historydetails>
    <taskEvent>
        <eventtype>Transitions</eventtype> 
        <historyevent>Task moved</historyevent> 
        <details>From 'Requested' to 'In Validation'</details> 
        <author>NAme</author> 
        <entrydate>01 Jul 13, 11:34</entrydate> 
       <historyid>2620</historyid> 
    </taskEvent>
    <taskEvent>
      <eventtype>Updates</eventtype> 
      <historyevent>Subtask marked done</historyevent> 
      <details>Subtask: visualise status and versions</details> 
      <author>NAme2</author> 
      <entrydate>21 Jun 13, 10:16</entrydate> 
     <historyid>2588</historyid> 
    </taskEvent>
</historydetails>

The corresponding classes look like this:

public class historydetails
{
    [XmlElement("taskEvent")]
    List<taskEvent> eventList = new List<taskEvent>();
}

public class taskEvent
{
    string eventtype { get; set; }
    string historyevent { get; set; }
    string details { get; set; }
    string author { get; set; }
    string entrydate { get; set; }
    string historyid { get; set; }
}

the code to deserialise the xml (the string replacement contains the xml code):

XmlSerializer deserializer = new XmlSerializer(typeof(historydetails));                              
object obj = obj = deserializer.Deserialize(stringToStream(replacement));           
historydetails XmlData = (historydetails)obj;

The method stringToStream

private MemoryStream stringToStream(string input)
{
    byte[] byteArray = Encoding.ASCII.GetBytes(input);
    MemoryStream stream = new MemoryStream(byteArray);
    return stream;
}

The result that i get is as following: The object XmlData is made and there is a list of taskEvents. The problem is in the list itself: it is empty...

like image 912
user2645940 Avatar asked Aug 02 '13 13:08

user2645940


People also ask

How does DeserializeObject work?

In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it calls the static method DeserializeObject() of the JsonConvert class by passing JSON data. It returns a custom object (BlogSites) from JSON data.

Can JsonConvert DeserializeObject throw?

DeserializeObject can throw several unexpected exceptions (JsonReaderException is the one that is usually expected). These are: ArgumentException.

How do I use JSON deserializer?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

What is serializing and deserializing JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).


1 Answers

You have to make the members public

public class historydetails
{
    [XmlElement("taskEvent")]
    public List<taskEvent> eventList = new List<taskEvent>();
}

  public class taskEvent
{
    public string eventtype { get; set; }
    public string historyevent { get; set; }
    public string details { get; set; }
    public string author { get; set; }
    public string entrydate { get; set; }
    public string historyid { get; set; }
}
like image 140
Ondrej Svejdar Avatar answered Oct 24 '22 04:10

Ondrej Svejdar