0

我用于反序列化的 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;

       }
4

1 回答 1

0

在 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 类型变量

加载文档后,您可以像访问列表的任何其他成员一样访问任何节点,并访问它的属性、子节点等。

于 2013-06-25T13:17:30.103 回答