0

I was trying to deserailize an object to generic list, Property marked with [XmlElement("ProcessName")] return null with interface but it works when [XmlElement("ProcessName")] is applied to class property.When the [XmlElement("ProcessName")] is removed from class pName property it returns null even though interface is marked with [XmlElement("ProcessName")]

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfProcess xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Process>
<Id>1</Id>
<ProcessName>Top Mold</ProcessName>
<ProcessCode>120</ProcessCode>
</Process>
<Process>
<Id>2</Id>
<ProcessName>Bottom Mold</ProcessName>
<ProcessCode>10</ProcessCode>
</Process>
<Process>
<Id>5</Id>
<ProcessName>Cooling</ProcessName>
<ProcessCode>110</ProcessCode>
</Process>
<Process>
<Id>6</Id>
<ProcessName>Heating</ProcessName>
<ProcessCode>310</ProcessCode>
</Process>
</ArrayOfProcess>

Interface and class`

 public interface IProcess
{
    [XmlAttribute]
    int Id { get; set; }

    [XmlElement("ProcessName")]
    string pName { get; set; }

    [XmlAttribute]
    string ProcessCode  { get; set; }
}

public class Process : IProcess
{
    public int Id { get; set; }

    [XmlElement("ProcessName")]
    public string pName { get; set; }

    public string ProcessCode { get; set; }
}

   public List<T> GenericDeSerialize<T>(string xml)
    {

        //Creating XmlSerializer for the object
        XmlSerializer serializer = new XmlSerializer(typeof(List<T>));

        //Geting XMl from the file.
        TextReader tr = new StringReader(xml);

        //Deserialize back to object from XML
        List<T> b = (List<T>)serializer.Deserialize(tr);
        tr.Close();
        return b;
    }
  • related, if not a duplicate: http://stackoverflow.com/questions/10376675/xmlattribute-xmlelement-in-interface-element-name-not-changed-when-serialized?rq=1 – rene Dec 10 '16 at 09:15
  • XmlSerializerNamespaces does't exist in deserialize method. – Manioth Shijith Dec 10 '16 at 09:46

0 Answers0