I use MS Studio 2010 Express for Windows Phone to build the app. In my app, I got the http respond and read it in string then I deserializer into the class object. It works fine in the class without List collection as a property. When it deserializer the class with List property, it got the error like this
Error in line 12 position 5. Expecting state 'Element'.. Encountered 'EndElement' with name 'ContactList', namespace 'http://schemas.datacontract.org/2004/07/DataObjects’.
I think I can read the xml and assign the value into the class. I searched to read xml file and a lot of website mentions to use XDocument .Prase method or xlmDocument.Load. However event adding the System.Xml.Ling as reference , I still cannot see XDocument .Parse method or xlmDocument . Would someone tell me what I should do in order to assign the following value into the class?
There is my class object:
public class CallDetails
{
public int id { get; set; }
public string summary { get; set; }
public string errorMsg { get; set; }
public int parentCallid { get; set; }
public string parentCallURL { get; set; }
public string assignedTo { get; set; }
public string OrgName { get; set; }
public DateTime onHoldSince { get; set; }
public DateTime onHoldUntil { get; set; }
public string requester { get; set; }
public bool isOnHold { get; set; }
private List<Contact> m_ContactList = new List<Contact>();
public List<Contact> ContactList
{
get { return m_ContactList; }
}
}
There is the respond:
<?xml version="1.0" encoding="utf-8"?><CallDetails xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DataObjects">
<ContactList>
<Contact>
<Method>PriPhone</Method>
<Number>(604) 555-1234</Number>
</Contact>
<Contact>
<Method>Fax</Method>
<Number>(604)555-1234</Number>
</Contact>
</ContactList>
<errorMsg
i:nil="true" />
<id>0</id>
<isOnHold>false</isOnHold>
<onHoldSince>0001-01-01T00:00:00</onHoldSince>
<onHoldUntil>0001-01-01T00:00:00</onHoldUntil>
<parentCallURL>/Call/349551</parentCallURL>
<parentCallid>0</parentCallid>
<requester>Peter </requester>
<summary>Mobile Application Research</summary>
</CallDetails>
Just adding the reference to
System.Xml.Linqwon’t do it, you also need to reference the namespace in the class header:Then you can call something like
XDocument doc = XDocument.Parse(content);. However, from what I can tell, you are trying to deserialize data, so you might want to use theXmlSerializerclass instead for all the core work. You can find some code ‘ispiration’ here.