我在使用C#中的列表时遇到了一些问题。
我有这个班级本地
[XmlType("Local")]
[XmlRoot("Local")]
public class clsLocal
{
public clsLocal()
{
clsLocal local = new clsLocal();
}
[XmlElement("TypeCommunication")]
public ETypeCommunication TypeCommunication{ get; set; }
[XmlArray("DeviceList")]
[XmlArrayItem("Device")]
public DeviceList ListOfDevices{ get; set; }
}
我有这个班级序列化这个本地。
public void SerializarXML(string path)
{
project = new clsProject();
local = new clsLocal();
project .ListOfLocals.Add(new clsLocal(local));
project .ListOfLocals[0].ListOfDevices.Add(new clsDevice { Name = "First" });
XmlSerializer s = new XmlSerializer(typeof(clsProjeto));
TextWriter writer = new StreamWriter(diretorio);
s.Serialize(writer, projeto);
writer.Close();
}
那是我的项目类
[XmlType(TypeName = "Project")]
public class clsProject
{
[XmlElement("StopBits")]
public StopBits StopBits{ get; set; }
[XmlElement("DataBits")]
public EDataBits DataBits{ get; set; }
[XmlElement("Port")]
public EPort Port{ get; set; }
public Parity Parity{ get; set; }
public EBaudRatea baudRate;
[XmlElement("BaudRate")]
public EBaudRate BaudRate
{
get { return baudRate; }
set { baudRate = value; }
}
[XmlArray("ListOfLocals")]
[XmlArrayItem("Local")]
public LocalList ListOfLocals;
}
我在序列化过程中有两个类可以帮助它们是LocalList和DeviceList。这两个类只从clsLocal和clsDevice继承。
问题是这样,我的应用程序因为clsLocal类的构造函数中的clsLocal实例而陷入循环。我对如何解决这个问题没有任何想法,有人可以帮助我吗? :P
关于XML,他应该是:
<Project>
<Local>
<Local>
<Device>
-First
</Device>
</Local>
</Local>
<Local>
</Local>
</Project>