0

My XML string for deserializ

> <?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>

when try to deserilize it gives ERROR "There is an error in XML document (0, 0)"

My Class

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; }
   }

Method for deserilization

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;

       }
  • 1
    Does the XML string have the `>` preceding each line? Because if you are trying to deserialize it with those you are going to have issues. – BlargleMonster Jun 25 '13 at 13:10
  • The `InnerException` property of the exception you are getting should contain a more detailed error message. – MiMo Jun 25 '13 at 13:11
  • 1
    Check if your XML has a byte-order-mark at the top of the file/text, and if it does try [removing it](http://stackoverflow.com/questions/295472/how-do-i-remove-the-bom-character-from-my-xml-file) – Chris Sinclair Jun 25 '13 at 13:11
  • 2
    You don't include the serializing part, but my guess would be that you are de-serializing with a UTF8 encoding but the XML shows the encoding as UTF-16. Thus the first line, i.e. line 0, position 0, is invalid. –  Jun 25 '13 at 13:12
  • Check this line: `xmlns:xsd="http://www.w3.org/2001/XMLSchema">" POP3` It looks like there's an errant quotation mark there before the `` tag. – Chris Sinclair Jun 25 '13 at 13:13
  • Also, you are deseralizing into an EmailAccount instance, but the XML is a ReceiveAccountSetting instance. Are the two classes related? –  Jun 25 '13 at 13:19
  • Yes there two related class EmailAccouts and ReceiveaccountSetting [Serializable] public class ReceiveAccountSetting { /// /// Receiving Email /// //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; } } – vivek bajpai Jun 26 '13 at 05:09

1 Answers1

0

It might be easier to load your XML file into a XMLDocument in C# and then convert it into a list of XML nodes by using SelectNodes method. That way you can access every node individually and take the data you need. You can load your file with this code:

 Document = new XmlDocument();

        try
        {
            openFileDialog1.ShowDialog();
            Document.Load(openFileDialog1.FileName);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        ListOfNodes = Document.SelectNodes("ReceiveAccountSettings");

Where ListOfNodes is XmlNodeList type variable

After you load your document, you can access any node like any other member of list, and access it's attributes, child nodes etc.

NDraskovic
  • 706
  • 2
  • 22
  • 50