如何将XML文件反序列化为对象列表

时间:2015-04-09 04:41:49

标签: c# asp.net xml serialization deserialization

我需要帮助,我有XML文件,我已经序列化了一个名为Class 1的类,它包含另一个名为“service”的类类型的列表 将列表序列化为XML文件之后我试图将文件反序列化回到服务类型列表中

这是第1类代码:

public class Class1

{
    public Class1()
    {
        ServiceList = new List<service>();

    }
    public List<service> ServiceList { get; set; }
}

public class service
{
    public string ServiceName { get; set; }
    public string Symbol { get; set; }

}

这是我的序列化代码:

 var filestream = new FileStream("sth.xml", FileMode.Create);
        new XmlSerializer(typeof(Class1)).Serialize(filestream,c);
        filestream.Close(); 

这是XML数据:

<?xml version="1.0"?>
<Class1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ServiceList>
<service>
  <ServiceName>cool</ServiceName>
  <Symbol>v</Symbol>
</service>
<service>
  <ServiceName>cool</ServiceName>
  <Symbol>v</Symbol>
</service>
<service>
  <ServiceName>cool</ServiceName>
  <Symbol>v</Symbol>
</service>
<service>
  <ServiceName>cool</ServiceName>
  <Symbol>v</Symbol>
</service>
<service>
  <ServiceName>cool</ServiceName>
  <Symbol>v</Symbol>
</service>
<service>
  <ServiceName>cool</ServiceName>
  <Symbol>v</Symbol>
</service>
<service>
  <ServiceName>cool</ServiceName>
  <Symbol>v</Symbol>
</service>
<service>
  <ServiceName>cool</ServiceName>
  <Symbol>v</Symbol>
</service>
<service>
  <ServiceName>cool</ServiceName>
  <Symbol>v</Symbol>
</service>
<service>
  <ServiceName>cool</ServiceName>
  <Symbol>v</Symbol>
</service>
 </ServiceList>
</Class1>   

这是我的反序列化代码,它运行正常,但没有给我正确的输出

        XmlSerializer myDeserilizer = new XmlSerializer(typeof(List<service>) ,  new XmlRootAttribute("Class1"));
        FileStream myfilestream = new FileStream("sth.xml", FileMode.Open);
        var loadedData = (List<service>)myDeserilizer.Deserialize(myfilestream);
        myfilestream.Close();  

问题是在DE序列化之后没有得到列表 任何帮助?

2 个答案:

答案 0 :(得分:0)

重写您的代码,如下所示:

[XmlRoot(ElementName = "Class1"), XmlType("Class1")]
public class Class1
{
  public Class1()
  {
     ServiceList = new List<service>();

  }
  [XmlElement(ElementName = "ServiceList")
  public List<service> ServiceList { get; set; }
}

[XmlRoot(ElementName = "Service"), XmlType("Service")]
public class service
{
   [XmlElement(ElementName = "ServiceName")
   public string ServiceName { get; set; }

   [XmlElement(ElementName = "Symbol")
   public string Symbol { get; set; }
}

然后更改反序列化代码,如下所示:

XmlSerializer myDeserilizer = new XmlSerializer(typeof(Class1));
FileStream myfilestream = new FileStream("sth.xml", FileMode.Open);
var loadedData = myDeserilizer.Deserialize(myfilestream);
myfilestream.Close();  

它应该有效,我在当地测试

答案 1 :(得分:0)

您的反序列化过去存在问题,您可以直接更改反序列化部分。

        XmlSerializer myDeserilizer = new XmlSerializer(typeof(Class1));
        FileStream myfilestream = new FileStream("sth.xml", FileMode.Open);
        var loadedData = (Class1)myDeserilizer.Deserialize(myfilestream);
        var listOfServices = loadedData.ServiceList;
        myfilestream.Close();