Reputation: 3213
I would like to serialize and deserialize mixed data into XML. After some searches I found out that there were two ways of doing it: System.Runtime.Serialization.Formatters.Soap.SoapFormatter and System.Xml.Serialization.XmlSerializer. However, neither turned out to match my requirements, since:
I'm wondering if there exists an implementation that doesn't have these limitations? I have found attempts (for example CustomXmlSerializer and YAXLib as suggested in a related SO question), but they don't seem to work either.
I was thinking about writing such serializer myself (though it certainly doesn't seem to be a very easy task), but then I find myself limited by the CLR, as I cannot create object instances of types that won't have a paramaterless constructor, even if I'm using reflection. I recall having read somewhere that implementations in System.Runtime.Serialization somehow bypass the normal object creation mechanism when deserializing objects, though I'm not sure. Any hints of how could this be done?
(See edit #3)
Could somebody please point me to the right direction with this?
Edit: I'm using .NET 3.5 SP1.
Edit #2: Just to be clear, I'd like a solution that is as much like using BinaryFormatter as possible, meaning that it should require the least possible extra code and annotations.
Edit #3: with some extra Googling, I found a .NET class called System.Runtime.Serialization.FormatterServices.GetUninitializedObject that actually can return "zeroed" objects of a specified type, which is great help in deserialization (if I get to implement it myself). I'd still like to find an existing solution though.
Upvotes: 1
Views: 3646
Reputation: 161773
Depending on your .NET version and the complexity of your data, you may have luck using LINQ to XML to serialize:
internal class Inner
{
public int Number { get; set; }
public string NotNumber { get; set; }
}
internal class Outer
{
public int ID { get; set; }
public Dictionary<string, Inner> Dict { get; set; }
}
internal class Program
{
private static void Main()
{
var data = new Outer
{
ID = 1,
Dict =
new Dictionary<string, Inner>
{
{
"ABC",
new Inner
{
Number = 1,
NotNumber = "ABC1"
}
},
{
"DEF",
new Inner
{
Number = 2,
NotNumber = "DEF2"
}
}
}
};
var serialized =
new XDocument(new XElement("Outer",
new XAttribute("id", data.ID),
new XElement("Dict",
from i in data.Dict
select
new XElement(
"Entry",
new XAttribute(
"key", i.Key),
new XAttribute(
"number",
i.Value.Number),
new XAttribute(
"notNumber",
i.Value.
NotNumber)))));
Console.WriteLine(serialized);
Console.Write("ENTER to finish: ");
Console.ReadLine();
}
}
Result:
<Outer id="1">
<Dict>
<Entry key="ABC" number="1" notNumber="ABC1" />
<Entry key="DEF" number="2" notNumber="DEF2" />
</Dict>
</Outer>
Deserializing:
private static Outer Deserialize(XDocument serialized)
{
if (serialized.Root == null)
{
return null;
}
var outerElement = serialized.Root.Element("Outer");
if (outerElement == null)
{
return null;
}
return new Outer
{
ID =
int.Parse(
outerElement.Attribute("id").Value),
Dict =
outerElement.Element("Dict").
Elements("Entry").ToDictionary(
k => k.Attribute("key").Value,
v => new Inner
{
Number = Convert.ToInt32(v.Attribute("number").Value),
NotNumber = v.Attribute("notNumber").Value
})
};
}
Upvotes: 2
Reputation: 641
I suggest DataContractJsonSerializer, which gives shorter output and handles dictionaries better.
Upvotes: -1
Reputation: 38758
Implementing custom XML serialization is not too bad. You could implement IXmlSerializable
in the class that the normal XmlSerializer cannot support by default.
This CodeProject article has a good explanation of IXmlSerializable
, and this blog post provides another look at more or less the same thing.
Upvotes: 1
Reputation: 10859
Which version of the .NET Framework do you use? If you are on .NET 3.0 or higher, you may have luck with the DataContractSerializer or the NetDataContractSerializer. Both of those do serialize to XML but work quite different than the XmlSerializer.
Upvotes: 2
Reputation: 10635
Ive had great success using the datacontractserializer class.here
Here's a pretty good article comparing serializers link
Upvotes: 3