如何将 XML 文件反序列化为对象列表
How to deserialize XML file into a list of objects
我需要帮助,我有一个 XML 文件,我已经在一个名为 Class 1 的 class 上进行了序列化,其中包含另一个名为 class 类型的列表27=]
将列表序列化为 XML 文件后,我正在尝试将文件反序列化为服务类型列表
这是 class 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();
问题是反序列化后我没有得到列表
有帮助吗??
按如下方式重写代码并查看结果:
[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();
应该可以,我在本地测试过
你过去的反序列化有问题,你可以简单地改变你的反序列化部分,如下所示。
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();
我需要帮助,我有一个 XML 文件,我已经在一个名为 Class 1 的 class 上进行了序列化,其中包含另一个名为 class 类型的列表27=] 将列表序列化为 XML 文件后,我正在尝试将文件反序列化为服务类型列表
这是 class 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();
问题是反序列化后我没有得到列表 有帮助吗??
按如下方式重写代码并查看结果:
[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();
应该可以,我在本地测试过
你过去的反序列化有问题,你可以简单地改变你的反序列化部分,如下所示。
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();