0

My application has 2 parts, an android client and a windows print server coded in c#.

I used Xstream in java to convert my object to XML (in android). here's a part of it:

<ROOT>
  <id>1</id>
  <serial>92000</serial>
  <date>2013/2/15</date>

  <ITEMS>

    <ITEM>
      <name>/**SOMETHING**/</name>
      <idd>/**SOMETHING**/</idd>
      <pd>/**SOMETHING**/</pd>
      <ed>/**SOMETHING**/</ed>
    </ITEM>
    <ITEM>
      <name>/**SOMETHING**/</name>
      <idd>/**SOMETHING**/</idd>
      <pd>/**SOMETHING**/</pd>
      <ed>/**SOMETHING**/</ed>
    </ITEM>
    <ITEM>
      <name>/**SOMETHING**/</name>
      <idd>/**SOMETHING**/</idd>
      <pd>/**SOMETHING**/</pd>
      <ed>/**SOMETHING**/</ed>
    </ITEM>

  </ITEMS>

</ROOT>

as you can see, I have 2 object types, one ROOT object type, and a nested list of second object type named ITEMS. I can read the ROOT object's name,serial and date, but for the nested list of ITEMS object, it always return null.

the class for the Root class in c# is:

[XmlRoot("ROOT")]
    public class ROOT_CLASS
    {

        [XmlElement("id")]
        public string idVar{ get; set; }

        [XmlElement("serial")]
        public string serialVar{ get; set; }

        [XmlElement("date")]
        public string dateVar{ get; set; }

        [XmlArray("ITEMS")]
        [XmlArrayItem("ITEM")]
    public List<NESTED_CLASS> oi { get; set; }

    }

and here is the nested object class:

[XmlRoot("ITEM")]
    public class NESTED_CLASS
    {
        [XmlElement("name")]
        public string nameVV; { get; set; }
        [XmlElement("idd")]
        public string iddVV; { get; set; }
        [XmlElement("pd")]
        public string pdVV; { get; set; }
        [XmlElement("ed")]
        public string edVV; { get; set; }

    } 

okay, How can I deserialize the NESTED_CLASS list out of this xml? as I said, I always get a null out of it. please help me. thanks...

Bardya Momeni
  • 337
  • 4
  • 13

2 Answers2

0

Use XmlArray attribute

[XmlRoot(ElementName = "ROOT")]
public class Root
{
    public int id { get; set; }
    public int serial { get; set; }
    public string date { get; set; }

    [XmlArray(ElementName = "ITEMS")]
    [XmlArrayItem("ITEM")]
    public List<RootItem> Items { get; set; }
}

public class RootItem
{
    public string name { get; set; }
    public string idd { get; set; }
    public string pd { get; set; }
    public string ed { get; set; }
}
jezzarax
  • 417
  • 2
  • 10
0

Instead of doing this manually, you could also generate your C# class by using xsd.exe. This may be easier, and faster in case this xml changes over time; and you don't have to worry about problems you are having now.

See Generate C# class from XML for an example on how to do that.

Note that if you are working with very large xml files, this approach may not be the best one as it loads the entire xml into memory. If this is the case, you might want to use this approach instead.

Community
  • 1
  • 1
L-Four
  • 13,345
  • 9
  • 65
  • 109
  • this is good approach too, very flexible... but as a rookie, must take steps one by one, and create flexible solutions later on my progress, but thanks for the great answer – Bardya Momeni Mar 19 '13 at 10:48