1

I've been landed with a feed of XML data that I need to deserialise into objects in a Silverlight (v5) application. The data looks like:

<AgentState>
  <agentName>jbloggs</agentName>
  <extension>12345</extension>
  <currentlyIn>TestStatus</currentlyIn>
</AgentState>

I've created a class at the Silverlight side, and I'm trying to get this XML - which, you'll notice, is missing a declaration and a namespace - into objects.

StringReader sr = null;
string data = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
sr = new StringReader(data);
XmlSerializer xs = new XmlSerializer(typeof (AgentState));
AgentState agent = (AgentState) xs.Deserialize(sr);

.. but this throws an error an error in xml document (1,2), as it's missing the declaration. Even manually adding a dummy declaration gives further errors about missing namespaces.

I've found other questions about ignoring namespace/declarations in XML, but none of these seem to work in Silverlight.

Can anyone advise on the best way to get this XML deserialised into an object?

Community
  • 1
  • 1
KenD
  • 5,280
  • 7
  • 48
  • 85

3 Answers3

2

This seems to work:

    public class AgentState
    {
        public string agentName { get; set; }
        public string extension { get; set; }
        public string currentlyIn { get; set; }
    }

    static void Main(string[] args)
    {
        var s = @"<AgentState>
                    <agentName>jbloggs</agentName>
                    <extension>12345</extension>
                    <currentlyIn>TestStatus</currentlyIn>
                </AgentState>";

        XmlSerializer serializer = new XmlSerializer(typeof(AgentState));
        var ms = new MemoryStream(Encoding.UTF8.GetBytes(s));
        var obj = serializer.Deserialize(ms);
    }
Joey Gennari
  • 2,361
  • 17
  • 26
2

I'm wondering what issue you have with appending the xml declaration to the string. This appears to work ok:

[System.Xml.Serialization.XmlRootAttribute("AgentState")]
public class AgentState
{
    public string agentName {get; set;}
    public int extension {get; set;}
    public string currentlyIn {get; set;}
}

public void RunSerializer()
{
    System.Xml.Serialization.XmlSerializer agent_serializer =
       new System.Xml.Serialization.XmlSerializer(typeof(AgentState));

    string agent_state_text = File.ReadAllText(@"C:\Temp\AgentState.xml");
    Console.WriteLine(agent_state_text + Environment.NewLine);
    string xml_agent_state = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + agent_state_text;
    Console.WriteLine(xml_agent_state + Environment.NewLine);

    AgentState agent_state = new AgentState();
    using(StringReader tx_reader = new StringReader(xml_agent_state))
    {
        if (tx_reader != null)
        {
            agent_state = (AgentState)agent_serializer.Deserialize(tx_reader);
        }
    }
    Console.WriteLine(agent_state.agentName);
    Console.WriteLine(agent_state.extension);
    Console.WriteLine(agent_state.currentlyIn);
}

Output:

<AgentState>
  <agentName>jbloggs</agentName>
  <extension>12345</extension>
  <currentlyIn>TestStatus</currentlyIn>
</AgentState>

<?xml version="1.0" encoding="UTF-8"?>
<AgentState>
  <agentName>jbloggs</agentName>
  <extension>12345</extension>
  <currentlyIn>TestStatus</currentlyIn>
</AgentState>

jbloggs
12345
TestStatus
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • OK, I've tried this, but I get `System.InvalidOperationException: was not expected` – KenD Apr 09 '13 at 17:19
  • @KenD - Even when I change the AgentState.xml file to have `` it still seemed to work. Could you post the full XML? – SwDevMan81 Apr 09 '13 at 18:44
0

I've managed to get it working using the following code - I'm not convinced it's the "right" way to do things, but it seems to work:

 string data = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);

 var document = XDocument.Parse(data);
 AgentState agent= (from c in document.Elements()
                        select new AgentState()
                                   {
                                       agentName = c.Element("agentName").Value,
                                       extension = c.Element("extension").Value,
                                       currentlyIn=c.Element("currentlyIn").Value
                                   }).Single();

Thanks for the advice, it got me on the right track.

KenD
  • 5,280
  • 7
  • 48
  • 85