0

This is my xml parsor class:

public class DataHandler
    {
        static XmlSerializer serializer;

        public static void Initialize()
        {
            serializer = new XmlSerializer( typeof( dataset ) );
            serializer.UnknownNode += new XmlNodeEventHandler( serializer_UnknownNode );
            serializer.UnknownAttribute += new XmlAttributeEventHandler( serializer_UnknownAttribute );
        }
        public static void ReadXML( string filename )
        {
            FileStream fs = new FileStream( filename, FileMode.Open );
            dataset dt;
            dt = ( dataset )serializer.Deserialize( fs );

        }

        protected static void serializer_UnknownNode( object sender, XmlNodeEventArgs e )
        {
            Console.WriteLine( "Unknown Node:" + e.Name + "\t" + e.Text );
        }

        protected static void serializer_UnknownAttribute( object sender, XmlAttributeEventArgs e )
        {
            System.Xml.XmlAttribute attr = e.Attr;
            Console.WriteLine( "Unknown attribute " +
            attr.Name + "='" + attr.Value + "'" );
        }
    }

This is my class structure:

public class dataset
    {
        public string name;
        public string comment;
        public List<image> images;
    }       
    public class image
    {
        [XmlAttribute]
        public string file;
        public List<box> box;
    }
    public class box
    {
        [XmlAttribute]
        public string top;
        [XmlAttribute]
        public string left;
        [XmlAttribute]
        public string width;
        [XmlAttribute]
        public string height;
        public List<part> part;
    }
    public class part
    {
        [XmlAttribute]
        public string name;
        [XmlAttribute]
        public string x;
        [XmlAttribute]
        public string y;
    }

And this is my xml:

    <dataset>
    <name>name</name>
    <comment>comments</comment>
    <images>
      <image file='2007_007763.jpg'>
        <box top='233' left='309' width='45' height='44'>
          <part name='00' x='322' y='246'/>
          <part name='01' x='320' y='249'/>     
        </box>
        <box top='233' left='309' width='45' height='44'>
          <part name='00' x='322' y='246'/>
          <part name='01' x='320' y='249'/>     
        </box>
      </image>
      <image file='2007_007763.jpg'>
        <box top='233' left='309' width='45' height='44'>
          <part name='00' x='322' y='246'/>
          <part name='01' x='320' y='249'/>     
        </box>
        <box top='233' left='309' width='45' height='44'>
          <part name='00' x='322' y='246'/>
          <part name='01' x='320' y='249'/>     
        </box>
      </image>
    </images>
    </dataset>

Problem is that it reads multiple "image"s but it does not read "box"s.
dataset.images has values but
dataset.images[0].box is null.

I tried many things I could think of to solve this problem but couldnt find a solution..
How should I change the class structures?

Edited by Devon_3

2
Contributors
1
Reply
18
Views
10 Months
Discussion Span
Last Post by tinstaafl
0

Your problem appears to be with the classes. The following appears to work. I changed the base class name so that it would be less confusing. It's usually good practice to avoid using keywords for class and class member names.

[XmlRoot("ImageDataset")]
public class ImageDataset
{
    public string name;
    public string comment;
    [XmlArray("images"),XmlArrayItem("image")]
    public List<image> images;
}
public class image
{
    [XmlAttribute]
    public string file;
    [XmlElement("box")]
    public List<box> box;
}
public class box
{
    [XmlAttribute]
    public string top;
    [XmlAttribute]
    public string left;
    [XmlAttribute]
    public string width;
    [XmlAttribute]
    public string height;
    [XmlElement("part")]
    public List<part> part;
}
public class part
{
    [XmlAttribute]
    public string name;
    [XmlAttribute]
    public string x;
    [XmlAttribute]
    public string y;
}

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.