Advertisement
Guest User

Serialization

a guest
Nov 21st, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml.Serialization;
  4.  
  5. namespace TCPAgent
  6. {
  7.     [XmlRoot("IncomingMessage")]
  8.     public class TcpAgentXmlMessage
  9.     {
  10.         [XmlElement("facilitySMIS")]
  11.         public List<FacilitySmisXml> FacilitiesNodes { get; set; }
  12.     }
  13.  
  14.     public class FacilitySmisXml
  15.     {
  16.         public FacilitySmisXml()
  17.         {
  18.             FacilityId = null;
  19.             SystemId = null;
  20.         }
  21.  
  22.         [XmlAttribute("SystemId")]
  23.         public string SystemId { get; set; }
  24.  
  25.         [XmlAttribute("facilityId")]
  26.         public string FacilityId { get; set; }
  27.  
  28.         [XmlElement("parameterFacilitySMIS")]
  29.         public ParameterFacilitySMISXml ParameterFacilitySMIS { get; set; }
  30.     }
  31.  
  32.     public class ParameterFacilitySMISXml
  33.     {
  34.         public ParameterFacilitySMISXml()
  35.         {
  36.             ParamFacilityId = null;
  37.         }
  38.  
  39.         [XmlAttribute("paramFacilityId")]
  40.         public string ParamFacilityId { get; set; }
  41.  
  42.         [XmlElement("value")]
  43.         public ValueXml Value { get; set; }
  44.     }
  45.  
  46.     public class ValueXml
  47.     {
  48.         public ValueXml()
  49.         {
  50.             _dateFixation = new DateTime();
  51.             Value = null;
  52.             Description = null;
  53.         }
  54.  
  55.         private DateTime _dateFixation;
  56.  
  57.         [XmlAttribute("dateFixation")]
  58.         public string DateFixationString
  59.         {
  60.             get { return _dateFixation.ToString(); }
  61.             set { DateTime.TryParse(value, out _dateFixation); }
  62.         }
  63.  
  64.         [XmlIgnore]
  65.         public DateTime DateFixation
  66.         {
  67.             get { return _dateFixation; }
  68.             set { _dateFixation = value; }
  69.         }
  70.  
  71.         [XmlAttribute("value")]
  72.         public string Value { get; set; }
  73.  
  74.         [XmlAttribute("valueText")]
  75.         public string Description { get; set; }
  76.     }
  77. }
  78.  
  79. public static T XmlDeserializeFromString<T>(string objectData)
  80.         {
  81.             return (T)XmlDeserializeFromString(objectData, typeof(T));
  82.         }
  83.  
  84. private static object XmlDeserializeFromString(string objectData, Type type)
  85.         {
  86.             var serializer = new XmlSerializer(type);
  87.             object result;
  88.  
  89.             using (TextReader reader = new StringReader(objectData))
  90.             {
  91.                 result = serializer.Deserialize(reader);
  92.             }
  93.  
  94.             return result;
  95.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement