ASP Forum
XML Serialization Question
Kodo | Posted 2:18pm 22. October 2005 Server Time |

I'm going to keep searching for the answer on this, but it would be helpful if anyone had any pointers.

Lets take a basic xml file

<ParentNode>
  <Child1>
     <ChildInfo>Some data<ChildInfo/>
  <Child1/>
  <Child2>
     <ChildInfo>Some data<ChildInfo/>
  <Child/2>
<ParentNode/>

I would like to have a serializable class that can produce this output. I'm not sure how to go about creating a class or method that can implement the hierarchical structure like that..

Any pointers welcome :)
Informant | Posted 9:59am 25. October 2005 Server Time |

using System;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
[XmlRoot]
public class ParentNode
{
public class Child1Helper
{
private string _childInfo = string.Empty;

public string ChildInfo
{
get{return this._childInfo;}
set{this._childInfo = value;}
}
}
public class Child2Helper
{
private string _childInfo = string.Empty;

public string ChildInfo
{
get{return this._childInfo;}
set{this._childInfo = value;}
}
}

private Child1Helper _child1 = null;
public Child1Helper Child1
{
get
{
if(_child1==null)
{
_child1 = new Child1Helper();
}
return _child1;
}
set{_child1 = value;}
}

private Child2Helper _child2 = null;
public Child2Helper Child2
{
get
{
if(_child2==null)
{
_child2 = new Child2Helper();
}
return _child2;
}
set{_child2 = value;}
}
public static void Main()
{

XmlSerializer xs = new XmlSerializer(typeof(ParentNode));

ParentNode pn = new ParentNode();

pn.Child1.ChildInfo = "Some data";
pn.Child2.ChildInfo = "Some data";

XmlTextWriter xtw = new XmlTextWriter("C:\\Serialized.xml", System.Text.Encoding.UTF8);

xs.Serialize(xtw,pn);


}

}
Kodo | Posted 1:21pm 25. October 2005 Server Time |

Ok, I took a peek at the code and it's not exactly what I am aiming for. I'm looking to loop through the child nodes because I don't know how many there will be.

How do I create a property that can do this?
Informant | Posted 9:24pm 27. October 2005 Server Time |

you can use a collection like an ArrayList
Kodo | Posted 10:47pm 29. October 2005 Server Time |

I was completely over analyzing the way to do this..
arraylist worked perfectly but you knew it would of course :)



Reply to Post XML Serialization Question



Back to Forum Page