views:

79

answers:

1

Hello Guys, I have tried to use the xsd.exe tool to generate a class for the following Oracle-generated xml sample but always fail to get it right when I try to change oracle xml elements names to the class's names, specifically I'm not sure how to do it for the ROWSET and ROW part of it.

Being very new to the serialization/deserialization business I'm sure this is very easy to implement for someone with a bit of more knowledge of it, can some help me please.

I have created 2 classes for for Department and Employee:

[XmlRoot("ROWSET")] 
class Department 
{ 
    [XmlElement("DEPARTMENT_ID")] 
    string DepID { set; get; } 
    [XmlElement("DEPARTMENT_NAME")] 
    string DepName { set; get; } 
    [XmlElement("EMPLOYEES")] 
    Employee[] Employees { set; get; } 
} 
class Employee 
{ 
    [XmlElement("EMP_ID")] 
    string DepID { set; get; } 
    [XmlElement("EMP_NAME")] 
    string DepName { set; get; } 
}

But this fails to work, on runtime it complains it cannot find the ROW element, which to be honest I don't know how to tell in the code to ignore it and start with the next node. I know xsd.exe would help me but it generates lots of unwanted data plus leaves me to sort the names manually. I would really like to learn how to do this without relying on an automation tool.

Many thanks,

Maya

<?xml version="1.0"?>
<ROWSET>
  <ROW>
    <DEPARTMENT_ID>1</DEPARTMENT_ID>
    <DEPARTMENT_NAME>Sales</DEPARTMENT_NAME>
    <EMPLOYEES>
      <EMP>
        <EMP_ID>12</EMP_ID>
        <EMP_NAME>Fred</EMP_NAME>
      </EMP>
      <EMP>
        <EMP_ID>13</EMP_ID>
        <EMP_NAME>Hohn</EMP_NAME>
      </EMP>
    </EMPLOYEES>
  </ROW>
  <ROW>
    <DEPARTMENT_ID>2</DEPARTMENT_ID>
    <DEPARTMENT_NAME>Marketing</DEPARTMENT_NAME>
    <EMPLOYEES></EMPLOYEES>
  </ROW>
</ROWSET>
+1  A: 

You may need to create an additional class Row. Also make sure classes and properties are public. Here's an example of how to serialize a Department instance to the given XML structure using XmlSerializer:

[XmlRoot("ROWSET")]
public class Department
{
    [XmlElement("ROW")]
    public Row[] Rows { get; set; }
}

public class Row
{
    [XmlElement("DEPARTMENT_ID")]
    public string DepID { set; get; }

    [XmlElement("DEPARTMENT_NAME")]
    public string DepName { set; get; }

    [XmlArray("EMPLOYEES")]
    [XmlArrayItem("EMP")]
    public Employee[] Employees { set; get; }
}

public class Employee
{
    [XmlElement("EMP_ID")]
    public string EmpID { set; get; }

    [XmlElement("EMP_NAME")]
    public string EmpName { set; get; }
}

class Program
{
    static void Main()
    {
        var dep = new Department
        {
            Rows = new[] 
            {
                new Row 
                {
                    DepID = "1",
                    DepName = "Sales",
                    Employees = new[] 
                    {
                        new Employee 
                        {
                            EmpID = "12",
                            EmpName = "Fred"
                        },
                        new Employee 
                        {
                            EmpID = "13",
                            EmpName = "Hohn"
                        }
                    }
                },
                new Row 
                {
                    DepID = "2",
                    DepName = "Marketing",
                }
            }
        };
        var serializer = new XmlSerializer(dep.GetType());
        serializer.Serialize(Console.Out, dep);
    }

And to deserialize back to a Department instance:

var xml = 
@"<?xml version=""1.0""?>
<ROWSET>
  <ROW>
    <DEPARTMENT_ID>1</DEPARTMENT_ID>
    <DEPARTMENT_NAME>Sales</DEPARTMENT_NAME>
    <EMPLOYEES>
      <EMP>
        <EMP_ID>12</EMP_ID>
        <EMP_NAME>Fred</EMP_NAME>
      </EMP>
      <EMP>
        <EMP_ID>13</EMP_ID>
        <EMP_NAME>Hohn</EMP_NAME>
      </EMP>
    </EMPLOYEES>
  </ROW>
  <ROW>
    <DEPARTMENT_ID>2</DEPARTMENT_ID>
    <DEPARTMENT_NAME>Marketing</DEPARTMENT_NAME>
    <EMPLOYEES></EMPLOYEES>
  </ROW>
</ROWSET>";

using (var reader = new StringReader(xml))
{
    var department = (Department)serializer.Deserialize(reader);
}
Darin Dimitrov
Thank you so much Darin!
Maya