1

I have a normal list based in model like:

Model:

public class ProjectHistoryModel
{
    public int  JobNumber { get; set; }
    public int DesignKey { get; set; }
    public string ProjectName { get; set; }
}

In class I have a list of this model like:

public List<ProjectHistoryModel> ProjectHistoryModel = new List<ProjectHistoryModel>();

Then I save that list into xml file as:

Serialize list:

public static string SerializeObject<T>(this T value)
{
    if (value == null)
    {
        return string.Empty;
    }
    try
    {
        var xmlserializer = new XmlSerializer(typeof(T));
        var stringWriter = new StringWriter();
        using (var writer = XmlWriter.Create(stringWriter))
        {
            xmlserializer.Serialize(writer, value);
            return stringWriter.ToString();
        }
    }
    catch (Exception ex)
    {
        throw new Exception("An error occurred", ex);
    }
}

So I save list just sending it to that method as:

var historyXml = ProjectHistoryModel.SerializeObject();
XML.HistoryProjects = historyXml;
XML.SaveXML();

Now my question is: How can I deserialize this xml and convert to a list again? I try it something like this but I get stuck:

  public static List<string> Load()
        {
            var xmlList = XML.HistoryProjects;

            using (var stream = System.IO.File.OpenRead(FileName))
            {
                var serializer = new XmlSerializer(xmlList));
                return serializer.Deserialize(stream) as [xmlList];
            }
        }

Regards

Juan Pedro
  • 175
  • 9
  • 1
    Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. – gunr2171 Oct 10 '18 at 19:42
  • T needs to be ProjectHistoryModel[], but this cannot happen. Serializer expects only a single root node. So List ProjectHistoryModel need to be embedded in a root class. – jdweng Oct 10 '18 at 19:45
  • Where is your code! What have you tried so far to deserialize? – Roman Marusyk Oct 10 '18 at 19:49
  • 1
    I posted it @RomanMarusyk – Juan Pedro Oct 10 '18 at 19:52

1 Answers1

1

You just need to do the same thing in reverse, using a StringReader instead of a writer.

public static T DeserializeObject<T>(this string source) 
{
    if (string.IsNullOrEmpty(source))
    {
        return default(T);
    }
    try
    {
        var xmlserializer = new XmlSerializer(typeof(T));
        var stringReader = new StringReader(source);
        using (var reader = XmlReader.Create(stringReader))
        {
            var result = xmlserializer.Deserialize(reader);
            return (T)result;
        }
    }
    catch (Exception ex)
    {
        throw new Exception("An error occurred", ex);
    }
}

Then call it with:

    var input = new List<ProjectHistoryModel>();
    var serialized = input.SerializeObject();
    var output = serialized.DeserializeObject<List<ProjectHistoryModel>>();

Here is a link to a working example on DotNetFiddle.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • What type of object is `input.SerializeObject();` in my case `XML.HistoryProjects` is an string(serialized object). So if I try to use Deserialized method it throw " 'string' does not contain a definition for 'DeserializeObject' and no extension method 'DeserializeObject' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?" – Juan Pedro Oct 10 '18 at 21:49
  • Make sure you have defined `DeserializeObject` as an extension method and included its namespace in the code file where you need it. See [this question](https://stackoverflow.com/questions/8345598/cant-access-my-extension-method). – John Wu Oct 10 '18 at 21:51