list xml deserialization

This is a discussion on list xml deserialization within the C# Programming forums, part of the General Programming Boards category; Hi, I made an xsd (xml scheme) and made a class from that using xsd.exe. in the generated class I ...

  1. #1
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116

    list xml deserialization

    Hi,
    I made an xsd (xml scheme) and made a class from that using xsd.exe.
    in the generated class I got an array:
    public Items[];
    but I need something that I will be able to add and remove element from it and have a variable size. ( I read the array documentation and it says that arrays *do* have variable size but I could not figure out how it is)
    so I wanted to change the array to ArrayList but then I saw in the System.Xml.Serialization.Deserialize that ArrayList are not supported.

    What then can I do so I can have a list in a class that I can serialize and deserialize?
    thanks.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Generic lists help here. From what I can tell, you can't serialize/deserialize a generic list directly (confirmation anyone? the docs are a little um, sketchy) but i've had a class inheriting from a list that's serializable.

    Code:
    public Main(int args)
    {
      MyList newList = new MyList();
      
      // do the textbook serialization/deserialization for a normal class stating the type as MyList
    }
    
    public class MyList : List<string>
    {
      public MyList() // constructor
      {
      }
      public string this[int index]
      {
        // getters and setters
      }
    }
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    .NET makes a little too easy sometimes, yeah you can serialize lists.

    Code:
    [XmlRoot]
    public class Root
    {
      [XmlArray]
      [XmlArrayItem("V")]
      public List<string> Values;
    }
    the above will make the following XML file:
    Code:
    <Root>
      <Values>
        <V></V>
        <V></V>
      </Values>
    </Root>

  4. #4
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    OMG C# rules. serialization is such a pain in C++.
    Thank you both.
    I did not think/know about Lists.
    It works great with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21