将XML字符串反序列化到我的类类型EmailAccount的c#对象中
本文关键字:EmailAccount 对象 类型 字符串 XML 反序列化 我的 | 更新日期: 2023-09-27 18:19:46
用于反序列化的XML字符串
> <?xml version="1.0" encoding="utf-16"?> <ReceiveAccountSetting
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <POP3>POP3</POP3>
> <Server>webmail.in</Server>
> <AccountId>vivek.s@gmail.com</AccountId> <Password>123</Password>
> <EnableSSL>true</EnableSSL> <DefaultPort>25</DefaultPort>
> </ReceiveAccountSetting>
当尝试取消加密时,会出现错误"XML文档(0,0)中有错误"
我的班级
public class ReceiveAccountSetting
{
/// <summary>
/// Receiving Email
/// </summary>
//public int AccountType { get; set; }
public string POP3 { get; set; }
public string IMAP { get; set; }
public string Server { get; set; }
public string AccountId { get; set; }
public string Password { get; set; }
public bool EnableSSL { get; set; }
public int DefaultPort { get; set; }
}
除菌方法
public EmailAccount Deserialize(string xmlstring)
{
EmailAccount objEmail = new EmailAccount();
XmlSerializer serializer = new XmlSerializer(typeof(EmailAccount));
StringReader reader = new StringReader(xmlstring);
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlstring)))
{
objEmail = (EmailAccount)serializer.Deserialize(stream);
}
objEmail = (EmailAccount)serializer.Deserialize(reader);
return objEmail;
}
在C#中将XML文件加载到XMLDocument中,然后使用SelectNodes方法将其转换为XML节点列表可能会更容易。这样,您就可以单独访问每个节点并获取所需的数据。你可以用以下代码加载你的文件:
Document = new XmlDocument();
try
{
openFileDialog1.ShowDialog();
Document.Load(openFileDialog1.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
ListOfNodes = Document.SelectNodes("ReceiveAccountSettings");
其中ListOfNodes是XmlNodeList类型变量
加载文档后,您可以像列表中的任何其他成员一样访问任何节点,并访问其属性、子节点等。