I have a plain old XML-file. Through some XSD.EXE magic I made a model. Now I want to read the XML data into the model. Normally this is just XmlSerializer.Deserialize, but it keeps complaining about namespaces and whatnot.
Now here is the thing: I don't care about namespaces, or anything else in XML. I just want the deserialization to work with a "simple one-liner". I'm planning to parse a lot of XML in my life and I'm not interested in spending my time fighting a bloated format about details we both know aren't important.
So I'm looking for a XML Deserializer for .Net that removes the fuzz and simply sees a <obj> <Name> ... and puts its data into public string Name { get; set; }. It should not be more difficult than for example MyObj myObj = SimpleXml.Deserialize<MyObj>(xmlString);. Pretty much like JSON deserializers work.
Where can I find an easy to use XML deserializer like the one I described?
I do understand that this limits my XML reading capability.
I am using this kind of helpers for my UI clients.
public string Serialize<T>(T o)
{
var x = new XDocument();
using(var w = x.CreateWriter())
new XmlSerializer(typeof(T)).Serialize(w, o);
return x.ToString();
}
public T Deserialize<T>(string s)
{
return
(T)new XmlSerializer(typeof(T))
.Deserialize(XDocument.Parse(s)
.CreateReader());
}
Beware if you are using windows services, there is a known memory leak in Serialization: http://dotnetcodebox.blogspot.fr/2013/01/xmlserializer-class-may-result-in.html
To avoid this, I suggest you to replace the new XmlSerializer of the code above by a method that creates/retrieves XmlSerializers from a cache (a Dictionary for example) as it is explained in the blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With