XmlElement在接口

时间:2016-12-10 09:05:01

标签: c# xml-serialization

我试图将对象deserailize到泛型列表,标有[XmlElement(“ProcessName”)的属性]返回带接口的null但是当[XmlElement(“ProcessName”)]应用于类属性时它可以工作。当[ XmlElement(“ProcessName”)]从类pName属性中删除它返回null,即使接口标有[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>

接口和类`

 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;
    }

0 个答案:

没有答案