1

Here's my issue : I need to get a list of resources from a web services, and deserialize it into object. But it doesn't work, despite the facts my code worked with another xml file. So I can't figure why it doesn't work, and I'm stuck with that !

Here's the XML :

<ResourceDataSet xmlns="http://schemas.microsoft.com/office/project/server/webservices/ResourceDataSet/">
<Resources>
  <RES_UID>blabla</RES_UID>
  <RES_NAME>blabla</RES_NAME>
  <RES_CODE>blabla</RES_CODE>
  <RES_GROUP>blabla</RES_GROUP>
  <RES_COST_CENTER>blabla</RES_COST_CENTER>
</Resources>
<Resources>
  <RES_UID>blabla</RES_UID>
  <RES_NAME>blabla</RES_NAME>
  <RES_CODE>blabla</RES_CODE>
  <RES_GROUP>blabla</RES_GROUP>
  <RES_COST_CENTER>blabla</RES_COST_CENTER>
</Resources>
<Resources>
  <RES_UID>blabla</RES_UID>
  <RES_NAME>blabla</RES_NAME>
  <RES_CODE>blabla</RES_CODE>
  <RES_GROUP>blabla</RES_GROUP>
  <RES_COST_CENTER>blabla</RES_COST_CENTER>
</Resources>
</ResourceDataSet>

The class I want to deserialize into :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Threading.Tasks;
using System.Collections;

namespace TestWPF
{   
[Serializable()]
public class Employee
{
    [System.Xml.Serialization.XmlElement("RES_UID")]
    public int RES_UID { get; set; }

    [System.Xml.Serialization.XmlElement("RES_NAME")]
    public String RES_NAME { get; set; }

    [System.Xml.Serialization.XmlElement("RES_CODE")]
    public String RES_CODE { get; set; }

    [System.Xml.Serialization.XmlElement("RES_GROUP")]
    public String RES_GROUP { get; set; }

    [System.Xml.Serialization.XmlElement("RES_COST_CENTER")]
    public String RES_COST_CENTER { get; set; }

    public Employee()
    { }

    public Employee(int r_id, String res_name, String res_code, String res_group, String res_cost_center)
    {
        this.RES_UID = r_id;
        this.RES_NAME = res_name;
        this.RES_CODE = res_code;
        this.RES_GROUP = res_group;
        this.RES_COST_CENTER = res_cost_center;
    }
}

[Serializable()]
[System.Xml.Serialization.XmlRoot("ResourceDataSet")]
public class EmployeeList //: IEnumerator, IEnumerable
{
    public EmployeeList() {Items = new List<Employee>();}
    [XmlArray("ResourceDataSet")]
    [XmlArrayItem("Resources")]
    public List<Employee> Items {get;set;}
}
}

And the code I use to deserialize :

EmployeeList lstEmployee = null;

XmlSerializer xs = new XmlSerializer(typeof(ServersList));

StreamReader sr = new StreamReader("testEmployee.xml");
lstEmployee = (EmployeeList)serializer.Deserialize(sr);
reader.Close();
for (int i = 0; i < lstEmployee.Items.Count(); i++)
{
    MessageBox.Show(lstEmployee.Items[i].RES_NAME);
}

And when I try to launch I receive this error message :

And when I try to launch I receive this error message :

T2o
  • 915
  • 1
  • 7
  • 10
  • Hello, I'm not 100% sure, but I think you need the xml declaration at the top. (unless of course that's only a snippet. it would be good to see the rest if it is) – SmithMart Mar 24 '14 at 08:50
  • In fact, I can't modify the XML, because I receive it from a WeB Service that I call before – T2o Mar 24 '14 at 08:58

2 Answers2

2

Firstly your xml file is invalid - RES_UID is expecting an int, so even when you get your serialization working you'll run into that problem.

You're also not taking into account the namespace. The following class works:

[Serializable()]
public class Employee
{
    [System.Xml.Serialization.XmlElement("RES_UID")]
    public int RES_UID { get; set; }

    [System.Xml.Serialization.XmlElement("RES_NAME")]
    public String RES_NAME { get; set; }

    [System.Xml.Serialization.XmlElement("RES_CODE")]
    public String RES_CODE { get; set; }

    [System.Xml.Serialization.XmlElement("RES_GROUP")]
    public String RES_GROUP { get; set; }

    [System.Xml.Serialization.XmlElement("RES_COST_CENTER")]
    public String RES_COST_CENTER { get; set; }

    public Employee()
    { }

    public Employee(int r_id, String res_name, String res_code, String res_group, String res_cost_center)
    {
        this.RES_UID = r_id;
        this.RES_NAME = res_name;
        this.RES_CODE = res_code;
        this.RES_GROUP = res_group;
        this.RES_COST_CENTER = res_cost_center;
    }
}

[Serializable()]
[System.Xml.Serialization.XmlRoot("ResourceDataSet", Namespace = "http://schemas.microsoft.com/office/project/server/webservices/ResourceDataSet/")]
public class EmployeeList //: IEnumerator, IEnumerable
{
    public EmployeeList() {Items = new List<Employee>();}

    [XmlElement("Resources", Type = typeof(Employee))]
    public List<Employee> Items {get;set;}
}
}

and your calling code with the typos fixed:

EmployeeList lstEmployee = null;

            XmlSerializer xs = new XmlSerializer(typeof(EmployeeList));

            StreamReader sr = new StreamReader("testEmployee.xml");
            lstEmployee = (EmployeeList)xs.Deserialize(sr);
            sr.Close();
            for (int i = 0; i < lstEmployee.Items.Count(); i++)
            {
                MessageBox.Show(lstEmployee.Items[i].RES_NAME);

            }

Remember to fix your xml to be ints otherwise it still won't work

NDJ
  • 5,189
  • 1
  • 18
  • 27
  • Yes, it perfectly works ! I'll examine the changes you made to fully understand what was the problem, but thank's a lot, it's a great answer. – T2o Mar 24 '14 at 09:54
0

You need to either decorate your root entity with the XmlRoot attribute or Or specify the root attribute when de serializing at runtime.

Here is a thread about this issue https://stackoverflow.com/a/1557145/1305119

Community
  • 1
  • 1
Suresh Kumar Veluswamy
  • 4,193
  • 2
  • 20
  • 35
  • I decorate the root entity (see just above my EmployeeList class) – T2o Mar 24 '14 at 08:59
  • I think you should check the code... the Xmlserializer should be initialized with typeof EmployeeList.... XmlSerializer xs = new XmlSerializer(typeof(EmployeeList)); – Suresh Kumar Veluswamy Mar 24 '14 at 09:03
  • Indeed, I made a mistake with XmlSerializer initialization, but it doesn't work either with typeof(employeeList) ! I think there's a problem with my xml, but I can't see it. – T2o Mar 24 '14 at 09:10
  • I think the serializer is not able to locate the root... Try this....XmlRootAttribute root = new XmlRootAttribute("ResourceDataSet"); root.Namespace =the url in the xml; XmlSerializer xs = new XmlSerializer(typeof(EmployeeList),root); – Suresh Kumar Veluswamy Mar 24 '14 at 09:50