[.net] [C#] Xml serializing woes

Started by
6 comments, last by Rainweaver 15 years, 10 months ago
I'm having issues in using Xml.Serialization. It's quite possible that I'm just not understanding everything, so I'm making stupid mistakes. The serialiser seems to be ignoring (or not noticing) my XmlAttribute tags. All the xml elements get created as I expect, but no information is stored with them, as I'm currently storing the info in the attributes.

[XmlRoot("FileGroupDatabase")]
public class FileGroupManager
{
    private List<FileGroup> _FileGroupList;

    [XmlArray("FileGroups"), XmlArrayItem("FileGroup")]
    public List<FileGroup> GroupList
    {
        get
        {
            return _FileGroupList;
        }
    }
}

public class FileGroup : FileGroupBaseAttributes 
{
    private List<File> _Files;

    [XmlArray("Files"), XmlArrayItem("File")]
    // the list of files in this group
    public List<File> Files
    {
        get
        {
            return _Files;
        }
    }
}

public class File  :    FileGroupBaseAttributes 
{
    // the full path of the file
    private string _Path;

    [XmlAttribute("path")]
    public string Path
    {
        get
        {
            return _Path;
        }
    }
}

public class FileGroupBaseAttributes
{
    // the list of tags for this item
    private List<string> _Tags;

    // the name
    private string _Name;

    // return a string that contains all the tags seperated by a space char
    [XmlAttribute("tags")]
    public string TagStr
    {
        get
        {
            // blah
        }
    }

    [XmlAttribute("name")]
    public string Name
    {
        get
        {
            return _Name;
        }
    }
}

And this is the sort of thing that I get output:

<FileGroupDatabase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FileGroups>
    <FileGroup>
      <Files>
        <File />
        <File />
        <File />
        <File />
      </Files>
    </FileGroup>
  </FileGroups>
</FileGroupDatabase>

And I have verified that the data members that the properties reference do have data in them right before they are serialized. What am I missing? I thought that my attempted usage would have been a pretty average case.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
Afaik, XML serializer doesn't like properties... so that's something you might want to check. Upgrade your private vars to public. Make sure you also have a parameterless constructor which initializes your collections, so that the serializer can call it before actually populating the lists.

Hope this helps.
Rainweaver Framework (working title)

IronLua (looking for a DLR expert)



Quote:Original post by Rainweaver
Afaik, XML serializer doesn't like properties... so that's something you might want to check. Upgrade your private vars to public. Make sure you also have a parameterless constructor which initializes your collections, so that the serializer can call it before actually populating the lists.

Hope this helps.


Please do not follow any of this advice. It is really a bad idea.

First of all, XML serialization works just fine with properties as long as you understand what is going on.

When you have a collection if have a bit of a different issue. The XmlSerializer only serializes the elements in the collection when it detects either the IEnumerable or the ICollection interface. So if you don't have either then it skips those items in your collection.

So you have two choices, either add IEnumerable to your FileGroup class (this is my recommendation) or do custom serialization. Do do custom you need to inherit from ISerializable interface, then you need to make a serilization constructor with the two parmaters; SerializationInfo info, StreamingContext context, finally need to override void GetObjectData(SerializationInfo si, StreamingContext context). That is it.

theTroll


Quote:Original post by TheTroll
Do do custom you need to inherit from ISerializable interface, then you need to make a serilization constructor with the two parmaters; SerializationInfo info, StreamingContext context, finally need to override void GetObjectData(SerializationInfo si, StreamingContext context). That is it.

You can also use the IXmlSerializable interface, overriding the GetSchema, ReadXml and WriteXml methods.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

IIRC the XmlSerializer only serializes public read/write properties (maybe except for collection properties). You might try adding a set routine for the File.Path property.
Quote:Original post by kanato
IIRC the XmlSerializer only serializes public read/write properties (maybe except for collection properties). You might try adding a set routine for the File.Path property.


This was it. I just added sets for my properties and they were all written to the xml file.

Is it possible to leave some of these properties as private and still have it serialize? I think I read about a deep serialize instead of a shallow one?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Yes you can serialize, but you either have to do binary serialization or override and roll your own.

theTroll
Quote:Original post by TheTroll
Please do not follow any of this advice. It is really a bad idea.
theTroll


Clearly you need to reconsider the fact that sometimes you want to prefer simplicity over intricacy, especially in cases such as the op's.

Still, I'm mesmerized by the quality of the information you provided, and I can only suggest the op to follow all of your advice.

Rainweaver Framework (working title)

IronLua (looking for a DLR expert)



This topic is closed to new replies.

Advertisement