这是反序列化的代码
UpdatePCPwdConfiguration config = new UpdatePCPwdConfiguration();
XmlSerializer serializer = new XmlSerializer(typeof(UpdatePCPwdCollection));
FileStream stream = new FileStream(System.IO.Path.GetFullPath("PCIPList.config"), FileMode.Open);
config = (UpdatePCPwdConfiguration)serializer.Deserialize(stream);
代码应该没有问题,因为以前都这么用,没出错过!
这是XML的Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<PCConfiguration>
<pcAddress>
10.102.12.108
</pcAddress>
</PCConfiguration>
</configuration>请各位帮忙看看
UpdatePCPwdConfiguration config = new UpdatePCPwdConfiguration();
XmlSerializer serializer = new XmlSerializer(typeof(UpdatePCPwdCollection));
FileStream stream = new FileStream(System.IO.Path.GetFullPath("PCIPList.config"), FileMode.Open);
config = (UpdatePCPwdConfiguration)serializer.Deserialize(stream);
代码应该没有问题,因为以前都这么用,没出错过!
这是XML的Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<PCConfiguration>
<pcAddress>
10.102.12.108
</pcAddress>
</PCConfiguration>
</configuration>请各位帮忙看看
XmlSerializer serializer = new XmlSerializer(typeof(UpdatePCPwdCollection));
FileStream stream = new FileStream(System.IO.Path.GetFullPath("PCIPList.config"), FileMode.Open);
config = (UpdatePCPwdConfiguration)serializer.Deserialize(stream); 上面的代码是要把一个Xml文档反序列为一个对象,你的xml文档和对象的属性之间对应出了问题。
把xml文档改为如下应该就ok了:
<?xml version="1.0" encoding="utf-8" ?>
<PCConfiguration>
<pcAddress>
10.102.12.108
</pcAddress>
</PCConfiguration>
public class UpdatePCPwdCollection : CollectionBase
{
public void Add(UpdatePCPwdEntity updatePCPwd)
{
base.List.Add(updatePCPwd);
}
[XmlElement]
public UpdatePCPwdEntity this[int index]
{
get
{
return (UpdatePCPwdEntity)base.List[index];
}
set
{
base.List[index] = value;
}
}
}[Serializable, XmlRoot("configuration")]
public class UpdatePCPwdConfiguration
{
private UpdatePCPwdCollection updatePCPwd = new UpdatePCPwdCollection(); [XmlElement("PCConfiguration")]
public UpdatePCPwdCollection UpdatePCPwd
{
get
{
return this.updatePCPwd;
}
set
{
this.updatePCPwd = value;
}
}
}[Serializable]
public class UpdatePCPwdEntity
{
private string pcAddress; public UpdatePCPwdEntity()
{ } public UpdatePCPwdEntity(string pcAddress)
{
this.pcAddress = pcAddress;
} [XmlElement("pcAddress")]
public string PcAddress
{
get { return pcAddress; }
set { pcAddress = value; }
}
}这是序列化的代码
[Serializable]
public class UpdatePCPwdCollection : CollectionBase
{
public void Add(UpdatePCPwdEntity updatePCPwd)
{
base.List.Add(updatePCPwd);
}
[XmlElement]
public UpdatePCPwdEntity this[int index]
{
get
{
return (UpdatePCPwdEntity)base.List[index];
}
set
{
base.List[index] = value;
}
}
}[Serializable, XmlRoot("configuration")]
public class UpdatePCPwdConfiguration
{
private UpdatePCPwdCollection updatePCPwd = new UpdatePCPwdCollection(); [XmlElement("PCConfiguration")]
public UpdatePCPwdCollection UpdatePCPwd
{
get
{
return this.updatePCPwd;
}
set
{
this.updatePCPwd = value;
}
}
}[Serializable]
public class UpdatePCPwdEntity
{
private string pcAddress; public UpdatePCPwdEntity()
{ } public UpdatePCPwdEntity(string pcAddress)
{
this.pcAddress = pcAddress;
} [XmlElement("pcAddress")]
public string PcAddress
{
get { return pcAddress; }
set { pcAddress = value; }
}
}
改成下面的就可以了.XmlSerializer serializer = new XmlSerializer(typeof(UpdatePCPwdConfiguration));
帮忙看下啊,后来我改用xml序列化了,但是也有错误