All pastes #884710 Raw Edit

C# XML serialization, done right

public xml v1 · immutable
#884710 ·published 2008-01-30 15:26 UTC
rendered paste body
<!-- C# class:	public class A	{		A _Parent;		public A Parent		{			get { return _Parent; }			set { _Parent = value; }		}		List<A> _Children = new List<A>();		public List<A> Children		{			get { return _Children; }			set { _Children = value; }		}		string _Name = null;		public string Name		{			get { return _Name; }			set { _Name = value; }		}		public A(string name)		{			this.Name = name;		}		public A(string name, params A[] children)			: this(name)		{			foreach (A child in children)			{				this.Children.Add(child);				if (child != null)					child.Parent = this;			}			//this.Children.AddRange(children);		}		public override string ToString()		{			return this.Name ?? "<null>";		}	}--><!-- C# instance:A a = new A("root", new A("foo"), new A("bar"), null);--><?xml version="1.0"?><A name="A" type="Test.A" id="58225482">  <Parent name="Parent" type="Test.A" />  <Children name="Children" type="System.Collections.Generic.List`1[[Test.A, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" id="54267293">    <Count name="Count" type="System.Int32">3</Count>    <Item name="Item" type="Test.A" id="18643596">      <Parent name="Parent" type="Test.A" id="58225482" />      <Children name="Children" type="System.Collections.Generic.List`1[[Test.A, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" id="33574638">        <Count name="Count" type="System.Int32">0</Count>      </Children>      <Name name="Name" type="System.String" id="1502598398">foo</Name>    </Item>    <Item name="Item" type="Test.A" id="33736294">      <Parent name="Parent" type="Test.A" id="58225482" />      <Children name="Children" type="System.Collections.Generic.List`1[[Test.A, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" id="35191196">        <Count name="Count" type="System.Int32">0</Count>      </Children>      <Name name="Name" type="System.String" id="696029481">bar</Name>    </Item>    <Item name="Item" type="Test.A" />  </Children>  <Name name="Name" type="System.String">root</Name></A>