Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Deep xml serialize a struct Options

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 1.28 KB  |  views: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class Test
  2. {
  3.     private InnerTest innerTest;
  4.     private int testNumber;
  5.  
  6.  
  7.     public Test()
  8.     {
  9.         this.innerTest = new InnerTest();
  10.         this.testNumber = 5;
  11.     }
  12.  
  13.  
  14.     public int TestNumber1
  15.     {
  16.         get { return this.testNumber; }
  17.         set { this.testNumber = value;}
  18.     }
  19.  
  20.  
  21.     public InnerTest InnerTest
  22.     {
  23.         get { return this.innerTest; }
  24.         set { this.innerTest = value;}
  25.     }
  26.  
  27.  
  28. }
  29.  
  30.  
  31. public struct InnerTest
  32. {
  33.     private int number;
  34.  
  35.  
  36.     public InnerTest(int number)
  37.     {
  38.         this.number = number;
  39.     }
  40.     public int Number{get { return number; }}
  41. }
  42.        
  43. [DataMember]
  44. public int TestNumber1
  45. {
  46.     get { return this.testNumber; }
  47.     set { this.testNumber = value;}
  48. }
  49.  
  50. // note **not** a data-member
  51. public InnerTest InnerTest
  52. {
  53.     get { return this.innerTest; }
  54.     set { this.innerTest = value;}
  55. }
  56.  
  57. [DataMember]
  58. private int InnerTestValue
  59. {
  60.     get {return innerTest.Number;}
  61.     set {innerTest = new InnerTest(value);}
  62. }
  63.        
  64. Test test = new Test { TestNumber1 = 5 };
  65.  
  66. XmlSerializer xmlSer = new XmlSerializer(typeof(Test));
  67. MemoryStream memStm = new MemoryStream();
  68.  
  69. xmlSer.Serialize(memStm, test);
  70.        
  71. StreamReader stmR = new StreamReader(memStm);
  72. memStm.Position = 0;
  73. string output = stmR.ReadToEnd();
clone this paste RAW Paste Data