CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: XmlSerializer

  1. #1
    Join Date
    Jun 2009
    Posts
    18

    Unhappy XmlSerializer

    Good day,
    I have a class "TridaData" I want to serialize.
    Program I report an error on this line:

    XmlSerializer^ s = gcnew XmlSerializer (array<TridaDat^>::typeid);

    error:
    + NamespaceList 0x026f83c0 { _defaultCapacity= _items= _size= ...} System::Collections::ArrayList^

    full program:
    http://leteckaposta.cz/437977355

    Thank you very much for the advice.

    written in VS 2008, VC + +

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: XmlSerializer

    I don't believe you can use the XmlSerializer on an array of objects in the 2008 version of .Net. I tried your code in Visual Studio 2012 and it works fine.

    As a work-a-round, you could create a containing class with a property that is an array. Then you serialize this class.

    For example (C# notation)

    Code:
    [Serializable]
    public class TridaDats
    {
      [XmlElement]
      public TridaDat[] Entries { get; set; }
    }
    
    var xmlSerializer = new XmlSerializer(typeof(TridaDats));
    ...

  3. #3
    Join Date
    Jun 2009
    Posts
    18

    Re: XmlSerializer

    Program written in C # and it works to me.
    This way I got it:

    Code C#:
    Code:
    static public void SerializeToXML(TridaDat[] process, System.Windows.Forms.SaveFileDialog sfd)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(TridaDat[]));
                TextWriter textWriter = new StreamWriter(sfd.FileName);
                serializer.Serialize(textWriter, process);
                textWriter.Close();
            }
    I need to rewrite to VC+ +,to school.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: XmlSerializer

    XML-serializing an array shouldn't be a problem. In fact I do that myself all the time. However, most of the more advanced collections can't be XML-serialized that easily. If I need to serialize one of those, I've made the habit to first convert them to an array and then serialize that. Advanced features like implicit sorting or keyed access don't really need to be serialized anyway in most cases and can be reconstructed upon deserialization. And even though it's a rather plain kind of a collection, AFAIR an ArrayList is one of those that can't be XML-serialized, and I think that's what the error message is trying to tell the OP. Unlike the error message, the original code snippet doesn't mention that collection type, but I suspect the array item type TridaDat contains a field of that type.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured