3

I'm trying to serialize a part of a class from a C# Model to a XML File. However i'd like to do it with the less possible amount of code.

I currently have this : a class with a lot of properties (some of them are annotated with [XmlIgnore]) to serialize

public class MyClass
{
    public int id {get;set;}
    public string Title {get;set;}
    public string Body {get;set;}

    [XmlIgnore]
    public byte[] Image {get;set;}
    ...
}

the pattern i need to match

<Properties>
<Property Name="id">Value</Property>
<Property Name="Title">Value</Property>
<Property Name="Body">Value</Property>
...
</Properties>

the Name is the property in my c# model

The only things i found so far needs me to create a different class for that, and i don' t want to split my model in different sub classes. Do you know a way (Maybe with annotations) to create a custom serialization for this ?

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
dotdiego
  • 157
  • 3
  • 14
  • 1
    Duplicate of [Fast and compact object serialization in .NET](http://stackoverflow.com/questions/549128/fast-and-compact-object-serialization-in-net). – CodeCaster Feb 11 '14 at 14:08
  • 5
    Any reason why you are not using [XmlSerializer](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx)? Seems like the perfect choice... – James Feb 11 '14 at 14:08
  • 1
    I suggest a change of the question title: You are looking for something that is "efficient" in terms of your coding time, not "efficient" in terms of runtime or maintainability. As such, this question is also *not* a duplicate of the other question that explicitly wants runtime efficiency, as suggested by @CodeCaster. – O. R. Mapper Feb 11 '14 at 14:13
  • 1
    @CodeCaster: Not a duplicate of the question you refer to because the OP wants to have simple code, and not necessarily the fastest solution with the most compact representation. – Dirk Vollmar Feb 11 '14 at 14:30
  • @0xA3 OP does not show his code, yet asks how it can be simplified. Instead of rolling your own, using a library it's just a matter of calling `var serialized = SomeSerializer.Serialze(instance)`. It doesn't get simpler than that. – CodeCaster Feb 11 '14 at 14:33
  • Yes, i'm more in finding a way that does what i need (the pattern in the post) with what i already have (a model with properties to serialize) than to execute it faster. I used XmlSerializer, i'm going to watch about what Garath answered. – dotdiego Feb 11 '14 at 14:35
  • @Revv It's still not entirely clear to me what you want to accomplish and what you are having trouble with. Do you mean you don't want to use the `[XmlIgnore]` attribute? Do you mean you have a "whitelist" of property names which you want to serialize? – CodeCaster Feb 11 '14 at 14:38
  • 1
    @CodeCaster: This question explicitly asks for an Xml solution that is so flexible that it requires as little coding as possible, whereas the other question asks for an optimum of runtime and memory efficiency, with the highest rated answers pointing to non-Xml serialization methods. – O. R. Mapper Feb 11 '14 at 14:40
  • 1
    @O.R.Mapper I agree that it isn't a duplicate, retracted. – CodeCaster Feb 11 '14 at 14:44

3 Answers3

2

Try this: reflection of properties to XElement:

public static XElement ToXml<T>(T obj)
{
     return new XElement("Properties",
                         from pi in  typeof (T).GetProperties()
                         where !pi.GetIndexParameters().Any() 
                                && !pi.GetCustomAttributes(typeof(XmlIgnoreAttribute), false).Any()
                         select new XElement("Property"
                                             , new XAttribute("Name", pi.Name)
                                             , pi.GetValue(obj, null))
        );
}
Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
0

The easiest way is to implement IXmlSerializable. There is a nice tutorial how to do this on code project: How to customize XML serialization, but most code you will have to handle yourself. If it is possible I will suggest you to use standard serialization it is much more easy to do.

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
0

I usually use XmlSerializer (MSDN XmlSerializer Class)

Deserialize:

var ser = new XmlSerializer(typeof(MyType));

using (var fs = new FileStream("Resources/MyObjects.xml", FileMode.Open))
{
    var obj = ser.Deserialize(fs);
    var myObject = obj as myType;
    if(myObject != null)
        // do action
}

Serialize:

 var ser = new XmlSerializer(typeof(MyType));
 using (var fs = new FileStream("Resources/MyObjects.xml", FileMode.Create))
 {
     ser.Serialize(fs, myObject)
 }
csteinmueller
  • 2,427
  • 1
  • 21
  • 32
  • I'm exactly doing this. But the format isn't the one i need. i have a public class MyClass { public int id {get;set;} public string Title {get;set;} public string Body {get;set;} So the output isn't the same as the one i need – dotdiego Feb 11 '14 at 14:45
  • @Revv please update your question showing a class you'd like to serialize and the desired output. – CodeCaster Feb 11 '14 at 14:45
  • Is there a special reason why it should be this way? – csteinmueller Feb 11 '14 at 15:00
  • The xml in database must be the same to let the API works as well with new datas than with old ones. – dotdiego Feb 11 '14 at 15:15