0

deserializ の 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

XML ファイルを C# で 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 に答える