Question

I would like to import an xml file and deserialize it into my model objects.

I am using C# MVC4 Asp.Net 4.51

I have tried a number of methods - if someone can provide some guidance on the best way to achieve this it would be appreciated.

I have tried but cannot get this to work:

 public static KronosPunchRoundRuleSummary Deserialize()
 {
     XmlSerializer serializer = new XmlSerializer(typeof(KronosPunchRoundRuleSummary));
     TextReader textReader;

     textReader = new StreamReader(@"c:\WSAPunchRoundRule.xml");

     KronosPunchRoundRuleSummary summary = (KronosPunchRoundRuleSummary)serializer.Deserialize(textReader); 
     textReader.Close();
 }

I have also tried thisenter code here but I get an error saying it does not expect Kronos_WFC xmlns:

public static KronosPunchRoundRuleSummary Deserialize()
 {
     //XmlSerializer serializer = new XmlSerializer(typeof(KronosPunchRoundRuleSummary));
     //TextReader textReader;

 //textReader = new StreamReader(@"c:\WSAPunchRoundRule.xml");

 //KronosPunchRoundRuleSummary summary = (KronosPunchRoundRuleSummary)serializer.Deserialize(textReader); 
 //textReader.Close();

 XmlDocument doc = new XmlDocument();
 doc.Load(@"c:\WSAPunchRoundRule.xml");
 string xmlcontents = doc.InnerXml;


 return null;

}

The xml file looks like this

`<?xml version="1.0"?>

-<Kronos_WFC TimeStamp="03/12/2013 14:59 GMT+04:00" WFCVersion="6.2.11.231" VERSION="1.0">


-<Response action="RetrieveAllForUpdate" Status="Success">

<WSAPunchRoundRule OutPunchLateChangePoint="0:00" UnscheduledInGrace="0:00" OutPunchEarlyInsideRound="0:01" OutPunchEarlyOutsideGrace="0:00" Name="Default Early Start" OutPunchEarlyOutsideRound="0:01" UseScheduledOut="false" InPunchLateInsideGrace="0:00" InPunchLateInsideRound="0:01" OutPunchLateOutsideGrace="0:00" InPunchEarlyInsideGrace="0:00" UnscheduledInRound="0:01" InPunchEarlyChangePoint="2:00" InPunchLateOutsideGrace="0:00" InPunchLateOutsideRound="0:01" OutPunchLateInsideGrace="0:00" IsMissedOutException="true" TransferGrace="0:00" UnscheduledOutGrace="0:00" TransferRound="0:01" InPunchLateChangePoint="0:00" OutPunchLateInsideRound="0:01" OutPunchLateOutsideRound="0:01" OutPunchEarlyInsideGrace="0:00" OutPunchEarlyChangePoint="0:00" InPunchEarlyOutsideRound="0:01" InPunchEarlyOutsideGrace="0:00" UnscheduledOutRound="0:01" InPunchEarlyInsideRound="2:00"/>

<WSAPunchRoundRule OutPunchLateChangePoint="0:00" UnscheduledInGrace="0:00" OutPunchEarlyInsideRound="0:01" OutPunchEarlyOutsideGrace="0:00" Name="Ramadan" OutPunchEarlyOutsideRound="0:01" UseScheduledOut="false" InPunchLateInsideGrace="0:00" InPunchLateInsideRound="0:01" OutPunchLateOutsideGrace="0:00" InPunchEarlyInsideGrace="0:00" UnscheduledInRound="0:01" InPunchEarlyChangePoint="2:00" InPunchLateOutsideGrace="0:00" InPunchLateOutsideRound="0:01" OutPunchLateInsideGrace="0:00" IsMissedOutException="true" TransferGrace="0:00" UnscheduledOutGrace="0:00" TransferRound="0:01" InPunchLateChangePoint="0:00" OutPunchLateInsideRound="0:01" OutPunchLateOutsideRound="0:01" OutPunchEarlyInsideGrace="0:00" OutPunchEarlyChangePoint="0:00" InPunchEarlyOutsideRound="0:01" InPunchEarlyOutsideGrace="0:00" UnscheduledOutRound="0:01" InPunchEarlyInsideRound="2:00"/>

<WSAPunchRoundRule OutPunchLateChangePoint="0:00" UnscheduledInGrace="0:00" OutPunchEarlyInsideRound="0:01" OutPunchEarlyOutsideGrace="0:00" Name="Transfer Rounding" OutPunchEarlyOutsideRound="0:01" UseScheduledOut="true" InPunchLateInsideGrace="0:00" InPunchLateInsideRound="0:01" OutPunchLateOutsideGrace="0:00" InPunchEarlyInsideGrace="0:00" UnscheduledInRound="0:01" InPunchEarlyChangePoint="2:00" InPunchLateOutsideGrace="0:00" InPunchLateOutsideRound="0:01" OutPunchLateInsideGrace="0:00" IsMissedOutException="false" TransferGrace="0:00" UnscheduledOutGrace="0:00" TransferRound="0:01" InPunchLateChangePoint="0:00" OutPunchLateInsideRound="0:01" OutPunchLateOutsideRound="0:01" OutPunchEarlyInsideGrace="0:00" OutPunchEarlyChangePoint="0:00" InPunchEarlyOutsideRound="0:01" InPunchEarlyOutsideGrace="0:00" UnscheduledOutRound="0:01" InPunchEarlyInsideRound="2:00"/>

<WSAPunchRoundRule OutPunchLateChangePoint="0:00" UnscheduledInGrace="0:00" OutPunchEarlyInsideRound="0:01" OutPunchEarlyOutsideGrace="0:00" Name="Workrule Rounding" OutPunchEarlyOutsideRound="0:01" UseScheduledOut="false" InPunchLateInsideGrace="0:00" InPunchLateInsideRound="0:01" OutPunchLateOutsideGrace="0:00" InPunchEarlyInsideGrace="0:00" UnscheduledInRound="0:01" InPunchEarlyChangePoint="2:00" InPunchLateOutsideGrace="0:00" InPunchLateOutsideRound="0:01" OutPunchLateInsideGrace="0:00" IsMissedOutException="true" TransferGrace="0:00" UnscheduledOutGrace="0:00" TransferRound="0:01" InPunchLateChangePoint="0:00" OutPunchLateInsideRound="0:01" OutPunchLateOutsideRound="0:01" OutPunchEarlyInsideGrace="0:00" OutPunchEarlyChangePoint="0:00" InPunchEarlyOutsideRound="0:01" InPunchEarlyOutsideGrace="0:00" UnscheduledOutRound="0:01" InPunchEarlyInsideRound="2:00"/>

</Response>

</Kronos_WFC>`

My Class looks like this

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Web;
using System.Xml.Serialization;
using System.Data;
using System.Dynamic;
using System.Collections;
using System.IO;

namespace Mojito.Models
{
    public class KronosPunchRoundRuleSummary
    {
        public List<KronosPunchRoundRule> kronosPunchRoundRules { get; set; }
    }

public class KronosPunchRoundRule
{
    public virtual int KronosPunchRoundRuleId { get; set; }
    public virtual string Name { get; set; }
    public virtual string OutPunchLateChangePoint { get; set; }
    public virtual string UnscheduledInGrace { get; set; }
    public virtual string OutPunchEarlyInsideRound { get; set; }
    public virtual string OutPunchEarlyOutsideGrace { get; set; }
    public virtual string OutPunchEarlyOutsideRound { get; set; }
    public virtual string UseScheduledOut { get; set; }
    public virtual string InPunchLateInsideGrace { get; set; }
    public virtual string InPunchLateInsideRound { get; set; }
    public virtual string OutPunchLateOutsideGrace { get; set; }
    public virtual string InPunchEarlyInsideGrace { get; set; }
    public virtual string UnscheduledInRound { get; set; }
    public virtual string InPunchEarlyChangePoint { get; set; }
    public virtual string InPunchLateOutsideGrace { get; set; }
    public virtual string InPunchLateOutsideRound { get; set; }
    public virtual string OutPunchLateInsideGrace { get; set; }
    public virtual bool IsMissedOutException { get; set; }
    public virtual string TransferGrace { get; set; }
    public virtual string UnscheduledOutGrace { get; set; }
    public virtual string TransferRound { get; set; }
    public virtual string InPunchLateChangePoint { get; set; }
    public virtual string OutPunchLateInsideRound { get; set; }
    public virtual string OutPunchLateOutsideRound { get; set; }
    public virtual string OutPunchEarlyInsideGrace { get; set; }
    public virtual string OutPunchEarlyChangePoint { get; set; }
    public virtual string InPunchEarlyOutsideRound { get; set; }
    public virtual string InPunchEarlyOutsideGrace { get; set; }
    public virtual string UnscheduledOutRound { get; set; }
    public virtual string InPunchEarlyInsideRound { get; set; }

}   

}

Was it helpful?

Solution

To do it I suggest you to read XML Serializer and Deserializer Tutorials.

Hope helps. Greetings!

UPDATE:

To serialize a unknown object you must pass all object a add to the serializer to say hwo the must serialize:

public static void Save<T>(T item, string filename, IEnumerable<Type> typeList) where T : class, new()
    {

        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), typeList.ToArray());

        // To write to a file, create a StreamWriter object.
        StreamWriter writer = null;

        try
        {
            writer = new StreamWriter(filename);
            var ns = new XmlSerializerNamespaces();
            ns.Add(string.Empty, string.Empty);
            xmlSerializer.Serialize(writer, item, ns);
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (writer != null)
                writer.Close();
        }
    }

To Deserialize unknown object its the same:

public static T Load<T>(string filename, IEnumerable<Type> typeList) where T : class, new()
    {
        if (!File.Exists(filename))
            return new T();

        TextReader fileStream = null;
        try
        {
            // Construct an instance of the XmlSerializer with the type
            // of object that is being deserialized.

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), typeList.ToArray());

            // To read the file, create a FileStream.
            fileStream = new StreamReader(filename);

            return xmlSerializer.Deserialize(fileStream) as T;

            // Call the Deserialize method and cast to the object type.
            //  return xmlSerializer.Deserialize(fileStream) as T;
        }
        catch (Exception ex)
        {
            return new T();
        }
        finally
        {
            if (fileStream != null)
                fileStream.Close();
        }
    }

UPDATE2:

T -> is the object you want to serialize.

filname -> the path/destinaton where you whant tu save it.

IEnumerable -> is a list of Types. Only declare a IEnumerable and Add all you type you will serialize there and after pass it on the Save, Load method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top