Quantcast
Channel: DaniWeb Microsoft Windows Topics
Viewing all articles
Browse latest Browse all 2776

C# Xml.XmlAttribute class structure question

$
0
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?


Viewing all articles
Browse latest Browse all 2776

Trending Articles