Jump to content
  • Advertisement
Sign in to follow this  
Endar

[.net] [C#] Xml serializing woes

This topic is 3808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Yes you can serialize, but you either have to do binary serialization or override and roll your own.

theTroll

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!