none
"was not expected" message error - deserialized xml

Answers

  • You are missing namespace declarations in your class. You can treat element belonging to a namespace as if they had totally different name that elements that do not belong to the same namespace - even if the tag name looks the same. Take a look at code below (I created account class based on the xml fragment you had sent but I presume yours looks pretty much the same):

     

     

    Code Snippet

    [XmlRoot("account", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public class Account {

    private string parentConsumerId;

    private string fullName;

    private string description;

    private string telephone1;

    private string emailAddress1;

     

    [XmlElement("parentcostumerid", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public string ParentConsumerId {

    get { return parentConsumerId; }

    set { parentConsumerId = value; }

    }

     

    [XmlElement("fullname", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public string FullName {

    get { return fullName; }

    set { fullName = value; }

    }

     

    [XmlElement("description", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public string Description {

    get { return description; }

    set { description = value; }

    }

     

    [XmlElement("telephone1", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public string Telephone1 {

    get { return telephone1; }

    set { telephone1 = value; }

    }

     

    [XmlElement("emailaddress1", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public string EmailAddress1 {

    get { return emailAddress1; }

    set { emailAddress1 = value; }

    }

    }

     

    class test {

    public static void Main() {

    string xmlstring =

    @"<account xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">

    <parentcostumerid xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">f73877b0-a9a5-dd11-8d54-0003ff732565</parentcostumerid>

    <fullname xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">John Smith</fullname>

    <description xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">the one</description>

    <telephone1 xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">123456789</telephone1>

    <emailaddress1 xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">none</emailaddress1>

    </account>";

     

    XmlSerializer serializer = new XmlSerializer(typeof(Account));

    using(XmlReader xr = XmlReader.Create(new StringReader(xmlstring))) {

    Account account = (Account)serializer.Deserialize(xr);

    Console.WriteLine("ParentConsumerId: {0}\nFullName: {1}\nDescription: {2}\nTelephone: {3}\nEmailAddress: {4}",

    account.ParentConsumerId, account.FullName, account.Description, account.Telephone1, account.EmailAddress1);

    }

    }

    }

     

     

    Here is the result:

    ParentConsumerId: f73877b0-a9a5-dd11-8d54-0003ff732565
    FullName: John Smith
    Description: the one
    Telephone: 123456789
    EmailAddress: none

     

    One more thing - your xml fragment was actually not a valid Xml document/fragment. Some tags were not closed and some were closed like this <description />. The latter is fine if your element does not have any content if you open an element and put some content you need to use / character before the tag name like this:

    <description>This is a description</description>

    This <description /> is equivalent to <description></description>

     

    Hope this helps.

    Pawel

     

    Thursday, October 30, 2008 4:56 PM

All replies

  • Three things:

    1. What happens if you leave off the XmlRootAttribute?
    2. What is the definition of the "contact" type? In particular, does it define what namespace it's in
    3. There is a forum specifically about XML Serialization at http://social.msdn.microsoft.com/forums/en-US/asmxandxml/threads/

     

     

    Thursday, October 30, 2008 12:37 PM
  •  

    thank you for your reply

     

    1. the error remains the same

    2. "contact" is a data type from CRM, i believe the namespace is http://schemas.microsoft.com/crm/2007/WebServices

    3. I wasn't aware of that forum. I'll check it out.

     

     

    Once again, thanks

    Thursday, October 30, 2008 2:22 PM
  • You are missing namespace declarations in your class. You can treat element belonging to a namespace as if they had totally different name that elements that do not belong to the same namespace - even if the tag name looks the same. Take a look at code below (I created account class based on the xml fragment you had sent but I presume yours looks pretty much the same):

     

     

    Code Snippet

    [XmlRoot("account", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public class Account {

    private string parentConsumerId;

    private string fullName;

    private string description;

    private string telephone1;

    private string emailAddress1;

     

    [XmlElement("parentcostumerid", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public string ParentConsumerId {

    get { return parentConsumerId; }

    set { parentConsumerId = value; }

    }

     

    [XmlElement("fullname", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public string FullName {

    get { return fullName; }

    set { fullName = value; }

    }

     

    [XmlElement("description", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public string Description {

    get { return description; }

    set { description = value; }

    }

     

    [XmlElement("telephone1", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public string Telephone1 {

    get { return telephone1; }

    set { telephone1 = value; }

    }

     

    [XmlElement("emailaddress1", Namespace = "http://schemas.microsoft.com/crm/2007/WebServices")]

    public string EmailAddress1 {

    get { return emailAddress1; }

    set { emailAddress1 = value; }

    }

    }

     

    class test {

    public static void Main() {

    string xmlstring =

    @"<account xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">

    <parentcostumerid xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">f73877b0-a9a5-dd11-8d54-0003ff732565</parentcostumerid>

    <fullname xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">John Smith</fullname>

    <description xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">the one</description>

    <telephone1 xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">123456789</telephone1>

    <emailaddress1 xmlns=""http://schemas.microsoft.com/crm/2007/WebServices"">none</emailaddress1>

    </account>";

     

    XmlSerializer serializer = new XmlSerializer(typeof(Account));

    using(XmlReader xr = XmlReader.Create(new StringReader(xmlstring))) {

    Account account = (Account)serializer.Deserialize(xr);

    Console.WriteLine("ParentConsumerId: {0}\nFullName: {1}\nDescription: {2}\nTelephone: {3}\nEmailAddress: {4}",

    account.ParentConsumerId, account.FullName, account.Description, account.Telephone1, account.EmailAddress1);

    }

    }

    }

     

     

    Here is the result:

    ParentConsumerId: f73877b0-a9a5-dd11-8d54-0003ff732565
    FullName: John Smith
    Description: the one
    Telephone: 123456789
    EmailAddress: none

     

    One more thing - your xml fragment was actually not a valid Xml document/fragment. Some tags were not closed and some were closed like this <description />. The latter is fine if your element does not have any content if you open an element and put some content you need to use / character before the tag name like this:

    <description>This is a description</description>

    This <description /> is equivalent to <description></description>

     

    Hope this helps.

    Pawel

     

    Thursday, October 30, 2008 4:56 PM
  • Thank you Pawel.I got it now. You sure helped me Smile

     

    Thanks!!!

     

    Thursday, October 30, 2008 6:19 PM
  • Thanks Pawel.  You helped me quite a bit on this one.

     

    Tuesday, December 09, 2008 8:03 PM