3

I have a DateTime and a subclass that I want formatted specifically on XML serialization. Normally, without specifying anything, serialization of a DateTime would just follow the current culture, but I want DateTime formatted in a certain way (even if not deserializable), ditto the subclass.

So, given these classes:

public class MyClass
{
    public DateTime MyDateTime { get; set; }
    public MySubClass TheSubClass { get; set; }
}

public class MySubClass 
{
    public int ID { get; set; }
    public string Name { get; set; }
}

How do I specify serialization methods that would output:

<MyClass>
  <MyDateTime>2011-9-13T10:30:00Z</MyDateTime>
  <MySubClass>ID-Name</MySubClass>
</MyClass>

2 Answers 2

2

Are you utilizing XmlSerializer? If so, you do not need to include the [Serializable] attributes, they are ignored by XmlSerializer. You can customize the serialization by implementing the IXmlSerializable interface on your type.

http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Bad habit of mine, I always think [Serializable] is required. Thanks.
2

This is off the top of my head ...I haven't nested a type in Xml serialization as you have - but this should be close.

[XmlRoot]
public class MyClass
{
    [XmlElement]
    public DateTime MyDateTime { get; set; }
    [XmlElement]
    public MySubClass TheSubClass { get; set; }
}

[XmlRoot]
public class MySubClass 
{
    [XmlElement]
    public int ID { get; set; }
    [XmlIgnore]  // since you didn't include in XML snippet
    public string Name { get; set; }
}

If you are performing simple Xml serialization: check MSDN XmlSerializer.

Update

I missed I want DateTime formatted in a certain way ...what I've done is the following rather than implementing IXmlSerializable:

[XmlRoot]
public class MyClass
{
    [XmlElement]
    public string MyDateTime { get; set; }
    [XmlIgnore]
    public DateTime DT
    {
        get { /* return DateTime from MyDateTime */ }
        set { MyDateTime = value.ToString( /* use formatting */); } // ex. ToString("yyyy, MMMM dd : hh:mm")
    }
    [XmlElement]
    public MySubClass TheSubClass { get; set; }
}

4 Comments

Thanks. For Name it's actually part of the element, ID-Name in the MySubClass tag, but I got that figured out with luksan's answer.
@IAbstract [XmlElement] public class MySubClass results "Attribute 'XmlElement' is not valid on this declaration type. It is only valid on 'property, indexer, field, param, return' declarations."
@Beygi: In reference to the first example? I'll review ...but at first glance, you are correct.
Thanks for edit and good answer... i did +1 before, for this helpful post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.