c# - Is this too ... hacky? Xml to an object -


I am playing with my favorite thing, xml (please have me kill me) and the last goal is to save it Use more data in the house in another way (this is an export from other systems). I have found that works, but Meh, I think it can be much better.

  public state (idle element) {id = element.gate alimentbinnam ("id"); Title = element.GetElementByName ("title"); }  

I'm thinking of making it even more automated (Hami) represents data by adding data annotation to represent XML element, for example something like this.

  [XmlElement ("id")] public string id {get; Set; } [XmlElement ("title")] public string title {get; Set; }  

Then there is some reflection / mapper code to write but it seems both ways ... dirty should I take care of? Is there a better way? Probably deserialize is the right approach? I just feel like there is a mo bettaah way to do this.

You can use the square and to control the serialization and disassembly of objects, Markup the properties.

This is a simple method you can use to deerialize your XDocument in your object:

  public static t DeserializeXml & lt; T & gt; (XDocument document) {usage (var reader = document.CreateReader ()) {var serializer = new XmlSerializer (typeof (T)); Return (T) Serializer Dairyialize (Reader); }}  

and a simple serializer method:

  public stable string toxical & lt; T & gt; (T example) {(var output = new StringWriter (new stringbuilder ()) {var serializer = New XML capacitor (typef (t)); Serializer.Serialize (output, example); Return output. Toasting (); }}  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -